python 读取json大文件
奇葩json文件:{}{}{}{}{}
一行,919m
#!/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: yield part+'}' def main(): with open('./SA_SRN_RECORD.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()
感谢gaozengzeng老铁的解决方案。