簡體   English   中英

JSON 文件解析錯誤,同時嘗試使用 Python 打印某些值(json.decoder.JSONDecodeError:額外數據:第 2 行第 1 列(字符 20))

[英]JSON files parsing error while trying to print certain values using Python (json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20))

我是 Python 的新手,我想轉換一個 JSON 文件並將其打印到控制台。 當我嘗試打印整個 JSON 時,它正在拋出

json.decoder.JSONDecodeError:額外數據:第 2 行第 1 列(字符 20)

我的代碼:

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'])

我的 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"}

在您等待更好的答案時,此代碼(雖然不是很好)解決了您的問題:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM