Python讀取jsonlines格式文件
阿新 • • 發佈:2018-05-18
href 文檔 代碼 讀取 .json tar AR pri font
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 importjson_lines 2 3 with open(‘fileName.jsonl‘, ‘rb‘) as f: 4 for item in json_lines.reader(f): 5 print(item)
Python讀取jsonlines格式文件