用于使用python保存换行符分隔的json(又名linejson,jsonlines,.jsonl文件)的模式 [英] pattern for saving newline-delimited json (aka linejson, jsonlines, .jsonl files) with python

查看:290
本文介绍了用于使用python保存换行符分隔的json(又名linejson,jsonlines,.jsonl文件)的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python,我将json文档保存在这样的单独行中:

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')

然后像这样阅读:

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

轻松和简单使我认为必须要有一个陷阱?这就是linejson的全部名称,又名 jsonlines ?

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

推荐答案

是的,仅此而已.

这篇关于用于使用python保存换行符分隔的json(又名linejson,jsonlines,.jsonl文件)的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆