Python讀取jsonlines格式檔案

來源:互聯網
上載者:User

標籤:xxx   http   代碼   讀取   open   .com   ams   xxxx   其他   

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 jsonlines2 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_lines2 3 with open(‘fileName.jsonl‘, ‘rb‘) as f: 4    for item in json_lines.reader(f):5        print(item)

 

Python讀取jsonlines格式檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.