Skip to main content
 首页 » 编程设计

python之在 Python 中导入 JSON

2025年05月04日21pander-it

我想在 python 中导入位于“https://www.drivy.com/cars/458342/reviews?page=1&paginate_per=6&rel=next”的 JSON 文件。

当我运行这个时:

with open('C:/Users/coppe/Documents/py trials/eval.json') as json_file:   
reviews = json.load(json_file) 

我得到一个错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 6776: character maps to <undefined> 

实际上这个错误是由于 html 键值中包含的特殊字符引起的。知道这个角色是一个表情符号(拇指),我怎么能忽略这个来导入我的 JSON?

请您参考如下方法:

您需要为要使用的 json 编码器指定正确的格式。大多数使用 utf8,因此使用如下内容:

reviews = json.load( 
    open("C:/Users/coppe/Documents/py trials/eval.json", encoding="utf8") 
) 

或

with open('C:/Users/coppe/Documents/py trials/eval.json') as json_file: 
    reviews = json.load(json_file, encoding="utf8") 

祝你好运!