首页 > 解决方案 > 从 JSON 行文件中提取有用信息

问题描述

所以我有一个 JSON 行文件 (.jl),它在网络抓取时已输出到 Pycharm。

我想打印文件中每一行的文本、作者和标签。

首先,我只想逐行读取文件,并为文件的每一行使用 json 加载:

with open('quotes.jl') as jf:
   line = jf.readline()
   cnt = 1
   while line:
      x= json.loads(line)
      print(x['text'])
      print(x['author'])
      print(x['tags'])
      line = jf.readline()
      cnt += 1

该文件如下所示,这是前两行:

{"text": "The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.", "author": "Albert Einstein", "tags": ["change", "deep-thoughts", "thinking", "world"]}
{"text": "It is our choices, Harry, that show what we truly are, far more than our abilities.", "author": "J.K. Rowling", "tags": ["abilities", "choices"]} 

但我遇到以下错误:

  File "/Users/tomotto/PycharmProjects/scrapy/tutorial/json_test.py", line 7, in <module>
    x= json.loads(line)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None```


标签: pythonjson

解决方案


推荐阅读