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)

猜你喜欢

转载自www.cnblogs.com/sqtu/p/10568906.html