[英]JSON files parsing error while trying to print certain values using Python (json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20))
I am new to Python, and I want to convert a JSON file and print it to the console.我是 Python 的新手,我想转换一个 JSON 文件并将其打印到控制台。 When I try to print the whole JSON it is throwing
当我尝试打印整个 JSON 时,它正在抛出
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20)
json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 20)
My code:我的代码:
import json
with open('venv/Vt.json', 'r') as json_file:
parsed_json = json.load(json_file)
for idd in parsed_json:
print(idd['t_id'])
My JSON file:我的 JSON 文件:
{"index":{"_id":0}}
{"t_id":0,"timestamp":"2016-06-01T09:23:39Z","stat":"571","mod":"M02"}
{"index":{"_id":1}}
{"t_id":0,"timestamp":"2016-06-01T09:23:39Z","stat":"571","mod":"M02"}
While you wait for a better answer, this code (while not great) solves your issue:在您等待更好的答案时,此代码(虽然不是很好)解决了您的问题:
with open('venv/Vt.json', 'r') as json_file:
try:
t_ids = [json.loads(line)['t_id'] for line in json_file.readlines()]
print(t_ids)
except Exception:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.