美文网首页
流式切割文本

流式切割文本

作者: 高峥 | 来源:发表于2021-11-18 14:50 被阅读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()
    

    相关文章

      网友评论

          本文标题:流式切割文本

          本文链接:https://www.haomeiwen.com/subject/cpgdtrtx.html