繁体   English   中英

读取 Json 文件时出现 Python 回溯错误

[英]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 (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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM