def readmylines(f,mewline):
buf = ""
while True:
while newline in buf:
pos = buf.index(newline)#找到字符的位置
yield buf[:pos]
buf = buf[pos+len(newline):]
chunk = f.read(4096*5)#读取相应的字符长度
if not chunk:
#说明已经读到文件结尾
yield buf
break
buf += chunk
with open("file_name.txt")as f:
#假设以 \n 分割行
for line in readmylines(f, '\n'):
print(line)
网友评论