pattern for saving newline-delimited json aka linejson aka jsonlines with python

核能气质少年 提交于 2020-01-02 04:11:08

问题


With Python, I'm saving json documents onto separate lines like this:

from bson import json_util # pymongo

with open('test.json', 'ab') as f:
    for document in documents:
       f.write(json_util.dumps(document)+'\n')

and then reading like this:

with open('test.json') as f:
    for line in f:
        document = json_util.loads(line)

The ease and simplicity make me think that there must be a gotcha? Is this all there is to linejson, aka jsonlines?


回答1:


Yes, that's all there is to it.



来源:https://stackoverflow.com/questions/32242647/pattern-for-saving-newline-delimited-json-aka-linejson-aka-jsonlines-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!