使用 python 保存换行符分隔 json(又名行json、json行、.jsonl 文件)的模式

pattern for saving newline-delimited json (aka linejson, jsonlines, .jsonl files) with python

使用 Python,我将 json 文档保存到单独的行中,如下所示:

from bson import json_util # pymongo

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

然后像这样阅读:

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

轻松和简单让我觉得一定有陷阱?这就是第 json 行的全部内容,又名 jsonlines?

是的,仅此而已。