Python 读取 Json 文件时回溯错误

Python Traceback Error When Reading Json File

我刚刚在 json 文件中提取了我所有的高音扬声器历史记录。所以我想用 python 对推文进行一些数据分析。我打开终端并输入以下命令从 python.

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

并得到这个 "traceback" 错误

 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"必须added.You打开文件时指定编码:

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