gpt4 book ai didi

Python 读取 Json 文件时回溯错误

转载 作者:行者123 更新时间:2023-12-01 06:42:01 48 4
gpt4 key购买 nike

我刚刚将我所有的高音历史记录提取到一个 json 文件中。所以我想用python对推文做一些数据分析。我打开终端并输入以下命令以从 python 转储 json。

>>> import json
>>> with open('tweet.js') as json_file:
... data = json.load(json_file)
... print(data)

并收到此“回溯”错误

 Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Users\George\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\George\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 4771: character maps to <undefined>

json 文件名为 tweet.js,它遵循以下形式

{
"retweeted" : false,
"source" : "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
"entities" : {
"hashtags" : [ ],
"symbols" : [ ],
"user_mentions" : [ {
"name" : "Florin Pop \uD83D\uDC68\uD83C\uDFFB‍\uD83D\uDCBB",
"screen_name" : "florinpop1705",
"indices" : [ "0", "14" ],
"id_str" : "861320851",
"id" : "861320851"
} ],
"urls" : [ ]
},
"display_text_range" : [ "0", "155" ],
"favorite_count" : "0",
"in_reply_to_status_id_str" : "1194246195243302913",
"id_str" : "1200417547524493312",
"in_reply_to_user_id" : "861320851",
"truncated" : false,
"retweet_count" : "0",
"id" : "1200417547524493312",
"in_reply_to_status_id" : "1194246195243302913",
"created_at" : "Fri Nov 29 14:13:40 +0000 2019",
"favorited" : false,
"full_text" : "@florinpop1705 I've heard good things about it, but never tried it.... Using kdenlive is simple yet some things are difficult to implement like text effect",
"lang" : "en",
"in_reply_to_screen_name" : "florinpop1705",
"in_reply_to_user_id_str" : "861320851"
}

最佳答案

此解决方案将为您提供输出,必须添加encoding="utf8"。您在打开文件时指定编码:

import json
with open("tweet.json", encoding="utf8") as json_file:
data = json.load(json_file)
print(data)

关于Python 读取 Json 文件时回溯错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59411216/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com