Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从大文件创建较小的块并对块进行排序_Python_Python 2.7 - Fatal编程技术网

Python 从大文件创建较小的块并对块进行排序

Python 从大文件创建较小的块并对块进行排序,python,python-2.7,Python,Python 2.7,我正在用python实现外部排序,目前正面临这个问题。我已经将一个包含整数的大文本文件划分为小块,并尝试对这些小块进行排序。到目前为止,我能写这么多 with open(fpath,'rb') as fin: input_iter = iter(lambda: fin.read(40 * 1024),'') for item in input_iter: print item current_chunk = list(item) #

我正在用python实现外部排序,目前正面临这个问题。我已经将一个包含整数的大文本文件划分为小块,并尝试对这些小块进行排序。到目前为止,我能写这么多

with open(fpath,'rb') as fin:
    input_iter = iter(lambda: fin.read(40 * 1024),'')
    for item in input_iter:
        print item
        current_chunk = list(item)
        # sort the buffers
        current_chunk.sort(key = lambda x : int(x))
当我执行这段代码时,我得到了一个错误

File "problem3.py", line 68, in <lambda>
current_chunk.sort(key = lambda x : int(x))
ValueError: invalid literal for int() with base 10: ''
文件“problem3.py”,第68行,在
当前_chunk.sort(key=lambda x:int(x))
ValueError:基数为10的int()的文本无效:“”
我猜这是因为这一行
input\u iter=iter(lambda:fin.read(40*1024),“”)
这是解决这个问题的另一种方法。
谢谢

您的输入中有空格:

>>> int(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('\t')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
或者避免将它们转换为整数:

current_chunk.sort(key=lambda x: int(x) if x.strip() else x)

您的输入中有空格:

>>> int(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('\t')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
或者避免将它们转换为整数:

current_chunk.sort(key=lambda x: int(x) if x.strip() else x)

感谢您的帮助,但是我检查了文件,因为文件中没有空格,是“”(大括号之间没有空格)导致了问题。我们必须解决这个问题,“
过滤器(无,输入列表)
将为您清除空值。但是,您向我们展示的代码永远不会产生空字符串,空格(包括换行符和制表符)可以更好地解释错误。感谢您的帮助,但是我检查了文件,因为文件中没有空格,导致问题的是“”(大括号之间没有空格)。我们必须解决这个问题,“
过滤器(无,输入列表)
将为您清除空值。然而,您向我们展示的代码永远不会导致空字符串,空白(包括换行符和制表符)将更好地解释错误。