我试图在pycharm中使用python加载一个json文件,但似乎json.load()
并没有得到我的json格式。
我的json是这样的:
{"User_id":"304062","First_name":"client1_first_name ","Last_name":"client1_last_name","Email":"client1emailemailemail@gmail.com","City":"vitoria","Country":"country_code","Reservas":"0","Unsubscribe":"0"}
{"User_id":"1372","First_name"client2firstname".","Last_name":"client2lastname","Email":"tralala@blabla.com","City":"nop","Country":"bra","Reservas":"0","Unsubscribe":"0"}
我正在使用的代码如下:
import json
from pprint import pprint
with open('path_to_my_json/my_json.json',) as data_file:
data = json.load(data_file)
print(data)
pprint(data[0])
我收到的错误是:
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 341211 column 1 (char 163 - 58075195)
答案 0 :(得分:2)
这是无效的JSON:
{"User_id":"1372","First_name"client2firstname".","Last_name":"client2lastname","Email":"tralala@blabla.com","City":"nop","Country":"bra","Reservas":"0","Unsubscribe":"0"}
在加载JSON之前验证它: https://jsonlint.com/
答案 1 :(得分:0)
自从被问到已经有一段时间了,但我遇到了同样的问题并像这样解决了它:
tweets = []
for line in open('tweets.json', 'r'):
tweets.append(json.loads(line))
我在这里找到了这个解决方案:https://izziswift.com/python-json-loads-shows-valueerror-extra-data/(这是解决方案 2)。