如何一次读取文件 N 行? [英] How to read file N lines at a time?

查看:89
本文介绍了如何一次读取文件 N 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过一次最多读取 N 行来读取一个大文件,直到 EOF.在 Python 中最有效的方法是什么?类似的东西:

I need to read a big file by reading at most N lines at a time, until EOF. What is the most effective way of doing it in Python? Something like:

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

推荐答案

一个解决方案是列表推导式和切片运算符:

One solution would be a list comprehension and the slice operator:

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

在此 lines 之后是行元组.但是,这会将完整文件加载到内存中.如果您不想要这个(即如果文件可能非常大),还有使用生成器表达式和 islice 来自 itertools 包:

After this lines is tuple of lines. However, this would load the complete file into memory. If you don't want this (i.e. if the file could be really large) there is another solution using a generator expression and islice from the itertools package:

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

lines_gen 是一个生成器对象,它为您提供文件的每一行,并且可以在这样的循环中使用:

lines_gen is a generator object, that gives you each line of the file and can be used in a loop like this:

for line in lines_gen:
    print line

两种解决方案都给您最多 N 行(或更少,如果文件没有那么多).

Both solutions give you up to N lines (or fewer, if the file doesn't have that much).

这篇关于如何一次读取文件 N 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆