python读取以非换行符分隔的超大文件,并逐行输出

 

def myreadline(f, newline):
    buf = ""
    while True:
        while True:
            pos = buf.index(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 myreadline(f, "{|}":
        print (line)

 

posted on 2019-03-20 23:34  杜景喜  阅读(349)  评论(0编辑  收藏  举报