"""读取大文件,一行数据"""
def myreadlines(f,newline):
buf = ""
while True:
while newline in buf:
pos = buf.index(newline)
print(pos)
print(pos + len(newline))
yield buf[:pos]
buf = buf[pos+len(newline):]
chunk = f.read(4096)
if not chunk:
#说明已经读到文件结尾
yield buf
break
buf += chunk
with open("input.txt") as f:
for line in myreadlines(f,"{|}"):
print(line)
读取只有一行数据的大文件
最新推荐文章于 2024-08-20 13:04:52 发布