简体   繁体   English

需要 function 逐行读取大 txt 文件,如果没有更多行,打开文件并再次迭代

[英]Need function that read large txt file line by line and if no more lines, open file and iterate again

I have a large txt file and want to iterate over it.我有一个大的 txt 文件,想对其进行迭代。 Need a function that return me line by line like generator and if no more lines, open file and again iterate over it.需要一个 function 像生成器一样逐行返回我,如果没有更多行,打开文件并再次对其进行迭代。

file = open('names.txt', 'r')

def generator():
    for line in file:
        if line:
            return line.strip()
        else:
       ,,,,,,,, 
          

for i in range(100000000):
    a = generator()
    print(a)

If you only want to process small files, itertools.cycle as proposed by rdas is a nice option.如果您只想处理小文件,那么 rdas 提出的itertools.cycle是一个不错的选择。 The good point is that you could even use it on non seekable file objects, for example the ones produced from sockets.好处是您甚至可以在不可查找的文件对象上使用它,例如从 sockets 生成的文件对象。

But if you want to process large files with a little memory footprint, it is better to scan the file and rewind it:但是如果你想处理带有一点 memory 占用空间的大文件,最好扫描文件并倒带:

def line_generator(file):
    while True:
        for line in file:
            yield line.strip()
        file.seek(0)

with open('names.txt') as file:
    for i, line in enumerate(line_generator(file)):
        if i >= 100000000:
            print(line)

You used 100000000 but if you only print the lines, I would suggest a much smaller number...您使用了100000000但如果您只打印这些行,我建议您使用更小的数字...

Something like this doesn't require that the whole file is read into memory:这样的事情不需要将整个文件读入 memory:

def read_n(filepath, times=2):
    for _ in range(times):
        with open(filepath, "r") as fh:
            yield from fh

for line in read_n("file.txt"):
    print(line)

You could use itertools.cycle to create an infinite generator from the lines you read from the file.您可以使用itertools.cycle从您从文件中读取的行创建一个无限生成器。

from itertools import cycle


def file_loop(file_name):
    with open(file_name) as f:
        lines = f.readlines()
    return cycle(lines)


for line in file_loop('test.txt'):
    print(line)

Which would keep looping over all the lines indefinitely:这将无限期地循环遍历所有行:

line1
line2
...
linen
line1
line2
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM