[英]Python - Reading chunks and break if there is no lines
這段代碼逐行讀取一個大文件,處理每一行,然后在沒有新條目時結束該過程:
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:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.