Python读取jsonlines格式文件

Python读取jsonlines格式文件

json lines文件是一种便于存储结构化数据的格式,可以一次处理一条记录。可以用作日志文件或者其他。每条json数据之间存在一个"\n"分隔符。

具体信息可以查看http://jsonlines.org/

之前爬虫存储数据,使用了这个格式文件,但是在读取的时候,Python内置的json函数,会进行报错;

在网上找到了两个库:

1、jsonlines,文档:https://jsonlines.readthedocs.io/en/latest/

         github地址:https://github.com/wbolster/jsonlines

2、json-lines, github地址:https://github.com/TeamHG-Memex/json-lines

在Anaconda环境和Pycharm库安装中,暂时都无法搜到这两个库,因此只能使用pip命令

jsonlines具体读取代码如下:

1 import jsonlines
2 
3 with open("xxxx.jl", "r+", encoding="utf8") as f:
4     for item in jsonlines.Reader(f):
5         print(item)

json-lines具体读取代码:https://shamsurrahim.wordpress.com/2017/04/17/how-to-read-jsonl-file-in-python/

1 import json_lines
2 
3 with open('fileName.jsonl', 'rb') as f: 
4    for item in json_lines.reader(f):
5        print(item)

猜你喜欢

转载自www.cnblogs.com/lambs/p/9056823.html
今日推荐