如何每次读取文件N行?

49 投票
11 回答
71564 浏览
提问于 2025-04-16 16:39

我需要读取一个很大的文件,但每次最多只读取N行,直到文件结束(EOF)。在Python中,有什么有效的方法可以做到这一点?可以参考下面的代码:

with open(filename, 'r') as infile:
    while not EOF:
        lines = [get next N lines]
        process(lines)

11 个回答

17

这段代码可以处理文件中的任意行数和任何指定的 N 值。如果你的文件里有 1100 行,而 N = 200,那么你会得到 5 次处理 200 行的机会,还有一次处理 100 行。

with open(filename, 'r') as infile:
    lines = []
    for line in infile:
        lines.append(line)
        if len(lines) >= N:
            process(lines)
            lines = []
    if len(lines) > 0:
        process(lines)
23

在Python中,文件对象可以用来逐行读取文件内容。如果你想一次读取文件中的N行,可以使用文档中Itertools Recipes部分的grouper()函数。(你也可以看看如何以块的方式遍历列表的最“Pythonic”方法?

try:
   from itertools import izip_longest
except ImportError:  # Python 3
    from itertools import zip_longest as izip_longest

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(*args, fillvalue=fillvalue)

示例

with open(filename) as f:
     for lines in grouper(f, N, ''):
         assert len(lines) == N
         # process N lines here
48

一种解决方案是使用列表推导和切片操作符:

with open(filename, 'r') as infile:
    lines = [line for line in infile][:N]

这样处理后,lines 就变成了一组行的元组。不过,这样会把整个文件都加载到内存里。如果你不想这样做(比如说文件可能非常大),还有另一种方法可以使用生成器表达式和来自 itertools 包的 islice

from itertools import islice
with open(filename, 'r') as infile:
    lines_gen = islice(infile, N)

lines_gen 是一个生成器对象,它会逐行给你文件的内容,可以像这样在循环中使用:

for line in lines_gen:
    print line

这两种方法都可以让你获取最多 N 行(如果文件没有那么多行,可能会更少)。

撰写回答