python - 从 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```
解决方案
推荐阅读
- c# - 仅跨数据点移动垂直注释
- c# - 将 TimeSpan 转换为高于 23:59 的“hh:mm”
- wordpress - Mailchimp 表单发送到错误的列表
- firebase - Aws Lambda Firestore 请求超时
- azure - 根据链接的工作项 ID 查找变更集
- java - 使用 java 8 构建的第三方 gradle 插件并使用 java.sql 模块,如何使用 jdk 11 运行它的任务
- node.js - 如何在 node-express 应用程序中进行用户身份验证后显示/隐藏登录和注销
- react-native - React Native Scrollview中的scrollToEnd
- c - 如何通过cronjob调度定期调用C程序中的特定函数?
- python - 对象列表中的乘法总和