【发布时间】:2017-06-15 16:24:02
【问题描述】:
第三个链接似乎有答案,但它没有完成工作。
我无法找到将整个文件放入主内存的解决方案,因为我要处理的文件会非常大。所以我决定使用islice,如第三个链接所示。前 2 个链接无关紧要,因为它们仅用于 2 行或读取 1000 个字符。而我需要 1000 行。 for now N is 1000
我的文件包含 100 万 行:
示例:
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
因此,如果我一次阅读 1000 行,我应该通过 while 1000 次,但是当我打印 p 以检查我已经通过了多少次时,它不会停止在1000。在运行我的程序1400 秒后,它达到了19038838!!
代码:
def _parse(pathToFile, N, alg):
p = 1
with open(pathToFile) as f:
while True:
myList = []
next_N_lines = islice(f, N)
if not next_N_lines:
break
for line in next_N_lines:
s = line.split()
x, y, w = [int(v) for v in s]
obj = CoresetPoint(x, y)
Wobj = CoresetWeightedPoint(obj, w)
myList.append(Wobj)
a = CoresetPoints(myList)
client.compressPoints(a) // This line is not the problem
print(p)
p = p+1
c = client.getTotalCoreset()
return c
我做错了什么?
【问题讨论】:
-
f可能没有被消耗,因此您最终每次都读取相同和相同的 1000 行。这永远不会终止。您必须使用islice的替代公式(itertools.islice(iterable, start, stop[, step])这个而不是itertools.islice(iterable, stop)这个)
标签: python python-2.7