Python-读取块并在没有行时中断

Python-读取块并在没有行时中断,python,Python,这段代码逐行读取一个大文件,处理每一行,然后在没有新条目时结束处理: file = open(logFile.txt', 'r') count = 0 while 1: where = file.tell() line = file.readline() if not line: count = count + 1 if count >= 10:

这段代码逐行读取一个大文件,处理每一行,然后在没有新条目时结束处理:

file = open(logFile.txt', 'r')
count = 0

     while 1:
         where = file.tell()
         line = file.readline()
         if not line:
             count = count + 1
             if count >= 10:
               break
             time.sleep(1)
             file.seek(where)
         else:
            #process line 
根据我的经验,逐行阅读需要很长时间,因此我尝试改进此代码,以便每次都能阅读一大块行:

from itertools import islice
N = 100000
with open('logFile.txt', 'r') as file:
    while True:
       where = file.tell()
       next_n_lines = list(islice(file, N)).__iter__()
       if not next_n_lines:
          count = count + 1
          if count >= 10:
             break
          time.sleep(1)
          file.seek(where)
       for line in next_n_lines:
        # process next_n_lines

这可以很好地工作,除了结束部分,它不会结束进程(中断while循环),即使文件中没有更多的行。有什么建议吗?

原始代码已经一次读取了大量文件,它一次只返回一行数据。您刚刚添加了一个冗余生成器,它使用文件对象的读取行功能,一次读取10行

除了少数例外情况,迭代文件中的行的最佳方法如下所示

with open('filename.txt') as f:
    for line in f:
        ...
如果您需要一次迭代预设的行数,请尝试以下操作:

from itertools import islice, chain

def to_chunks(iterable, chunksize):
    it = iter(iterable)
    while True:
        first = next(it)
        # Above raises StopIteration if no items left, causing generator
        # to exit gracefully.
        rest = islice(it, chunksize-1)
        yield chain((first,), rest)


with open('filename.txt') as f:
    for chunk in to_chunks(f, 10):
        for line in chunk:
            ...

原始代码已经一次读取了大量文件,一次只返回一行数据。您刚刚添加了一个冗余生成器,它使用文件对象的读取行功能,一次读取10行

除了少数例外情况,迭代文件中的行的最佳方法如下所示

with open('filename.txt') as f:
    for line in f:
        ...
如果您需要一次迭代预设的行数,请尝试以下操作:

from itertools import islice, chain

def to_chunks(iterable, chunksize):
    it = iter(iterable)
    while True:
        first = next(it)
        # Above raises StopIteration if no items left, causing generator
        # to exit gracefully.
        rest = islice(it, chunksize-1)
        yield chain((first,), rest)


with open('filename.txt') as f:
    for chunk in to_chunks(f, 10):
        for line in chunk:
            ...

原始代码已经一次读取了大量文件,一次只返回一行数据。您刚刚添加了一个冗余生成器,它使用文件对象的读取行功能,一次读取10行

除了少数例外情况,迭代文件中的行的最佳方法如下所示

with open('filename.txt') as f:
    for line in f:
        ...
如果您需要一次迭代预设的行数,请尝试以下操作:

from itertools import islice, chain

def to_chunks(iterable, chunksize):
    it = iter(iterable)
    while True:
        first = next(it)
        # Above raises StopIteration if no items left, causing generator
        # to exit gracefully.
        rest = islice(it, chunksize-1)
        yield chain((first,), rest)


with open('filename.txt') as f:
    for chunk in to_chunks(f, 10):
        for line in chunk:
            ...

原始代码已经一次读取了大量文件,一次只返回一行数据。您刚刚添加了一个冗余生成器,它使用文件对象的读取行功能,一次读取10行

除了少数例外情况,迭代文件中的行的最佳方法如下所示

with open('filename.txt') as f:
    for line in f:
        ...
如果您需要一次迭代预设的行数,请尝试以下操作:

from itertools import islice, chain

def to_chunks(iterable, chunksize):
    it = iter(iterable)
    while True:
        first = next(it)
        # Above raises StopIteration if no items left, causing generator
        # to exit gracefully.
        rest = islice(it, chunksize-1)
        yield chain((first,), rest)


with open('filename.txt') as f:
    for chunk in to_chunks(f, 10):
        for line in chunk:
            ...


我不能完全确定,但我认为迭代器的计算结果总是
True
,因此
如果不是下一行:
是无用的。编辑:
bool(iter([])
返回
True
,所以这似乎确实是问题所在。@Rawing strange!那么你知道如何修复它吗?如果在下一行循环中只有一行,你可以省略
部分。我不是很确定,但我认为迭代器的计算结果总是
,所以
如果不是下一行:
是无用的。编辑:
bool(iter([])
返回
True
,所以这似乎确实是问题所在。@Rawing strange!那么你知道如何修复它吗?如果在下一行循环中只有一行,你可以省略
部分。我不是很确定,但我认为迭代器的计算结果总是
,所以
如果不是下一行:
是无用的。编辑:
bool(iter([])
返回
True
,所以这似乎确实是问题所在。@Rawing strange!那么你知道如何修复它吗?如果在下一行循环中只有一行,你可以省略
部分。我不是很确定,但我认为迭代器的计算结果总是
,所以
如果不是下一行:
是无用的。编辑:
bool(iter([])
返回
True
,所以这似乎确实是问题所在。@Rawing strange!那么你知道如何修复它吗?如果你在下一行循环中只有一行
,你可以省略
部分。对不起,我没有理解你的意思。您建议我在while循环中添加for循环吗?因为我需要使用seek函数来保持文件打开等待新数据,for loop with
line=file.readline()
给了我一个错误:
错误:混合迭代和读取方法会丢失数据
对不起,我没有理解你的意思。您建议我在while循环中添加for循环吗?因为我需要使用seek函数来保持文件打开等待新数据,for loop with
line=file.readline()
给了我一个错误:
错误:混合迭代和读取方法会丢失数据
对不起,我没有理解你的意思。您建议我在while循环中添加for循环吗?因为我需要使用seek函数来保持文件打开等待新数据,for loop with
line=file.readline()
给了我一个错误:
错误:混合迭代和读取方法会丢失数据
对不起,我没有理解你的意思。您建议我在while循环中添加for循环吗?因为我需要使用seek函数使文件保持打开状态,同时等待新数据,for循环with
line=file.readline()
给了我一个错误:
错误:混合迭代和读取方法会丢失数据