How to incrementally write into a json file

前端 未结 3 1766
我在风中等你
我在风中等你 2021-02-07 20:05

I am writing a program, which requires me to generate a very large json file. I know the traditional way is to dump a dictionary list using json.dump()

3条回答
  •  余生分开走
    2021-02-07 21:09

    Sadly the json library does not have any incremental writing facilities, and therefore cannot do what you want.

    That's clearly going to be a very large file - would some other representation be more appropriate?

    Otherwise the best suggestion I can make is to dump each list entry to an in-memory structure and write them out with the necessary delimiters ([ at the beginning, ],[ between entries and ] at the end) to try and construct the JSON that you need.

    If formatting is important you should know that the wrapper test your program writes will destroy correct indentation, but indentation is only for humans so it shouldn't make any difference to the semantics of the JSON structure.

提交回复
热议问题