Python 读取几百 GB 的文件,不爆内存

时间:2020-04-03 20:05:10   收藏:0   阅读:109
    buf = ‘‘
    while True:
        while newline in buf:
            pos = buf.index(newline)
            yield buf[:pos]
            buf = buf[pos + len(newline):]
        chunk = f.read(4096) # 每次读取的大小

        if not chunk:
            yield buf
            break
        buf += chunk
        

if __name__ == ‘__main__‘:
    # 文件中的分隔符
    flite = r"\n"
    with open("contain.txt") as f:
        for line in myreadlines(f, flite):
            print(line)

本文转载于 https://fishc.com.cn/thread-145508-1-1.html

原文:https://www.cnblogs.com/whx2008/p/12628542.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!