流式切割文本

2021-11-18  本文已影响0人  高峥
#!/usr/bin/env python3.8
# _*_ coding: utf-8 _*_
# Description:
# Author: gaozengzeng <gaozengzeng@sglcapital.com.cn>
# Date: 2021/11/18

def each_chunk(stream, separator):
    buffer = ''
    while True:  # until EOF
        chunk = stream.read(4096)  # I propose 4096 or so
        if not chunk:  # EOF?
            yield buffer
            break
        buffer = chunk
        while True:  # until no separator is found
            try:
                part, buffer = buffer.split(separator, 1)

            except ValueError:
                break
            else:
                print(1)
                yield part+'}'

def main():
    with open('/Users/gaozengzeng/Desktop/aa.json') as myFile:
        for chunk in each_chunk(myFile, separator='}'):
            print(chunk)  # not holding in memory, but printing chunk by chunk

if __name__ == '__main__':
    main()
上一篇下一篇

猜你喜欢

热点阅读