Python - 一次从文件中读取 1000 行

标签 python python-2.7

我检查过 this , thisthis .

第三个链接似乎有答案,但它没有完成工作。

我无法找到将整个文件放入主内存的解决方案,因为我要处理的文件会非常大。所以我决定使用 islice,如第 3 个链接所示。前 2 个链接无关紧要,因为它们仅用于 2 行或阅读 1000 个字符。而我需要 1000 行。 现在 N 是 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

我做错了什么?

最佳答案

正如@Ev.kounis 所说,您的 while 循环似乎无法正常工作。

我建议在这样的时间为数据 block 使用 yield 函数:

def get_line():
    with open('your file') as file:
        for i in file:
            yield i

lines_required = 1000
gen = get_line()
chunk = [next(gen) for i in range(lines_required)]

关于Python - 一次从文件中读取 1000 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41937144/

相关文章:

c++ - 嵌入式 Python 值冲突

python - 使用 nltk.corpus.gutenberg.fileids() 解码路径中的错误

python - 如何在 python 脚本内的子进程中使用 * ?

macos - Python basemap 导入错误: "requirement already satisfied"

python - 在golang中访问C数组

python - 刷新访问 token Python-Fitbit (Orcasgit)

Python 池 生成池

python - 在 Django 中创建用户后更新用户配置文件

python - 为什么导入并不总是导入嵌套包?

python - 保留拆分字符