如何在python中读取JSON文件时修复UnicodeDecodeError
我正在阅读python中的一个json文件,其中包含大量的进口商品数据以及它们的名称和税款。我想输入一个项目名称并搜索该文件以获取该项目的税款如何在python中读取JSON文件时修复UnicodeDecodeError,python,json,Python,Json,我正在阅读python中的一个json文件,其中包含大量的进口商品数据以及它们的名称和税款。我想输入一个项目名称并搜索该文件以获取该项目的税款 import json with open("./Files/CD_Data.json") as file: #item = input("Enter item name: ") reader = json.load(file) print(reader) 但当显示所有数据时,就会出现此错误 UnicodeDecodeErro
import json
with open("./Files/CD_Data.json") as file:
#item = input("Enter item name: ")
reader = json.load(file)
print(reader)
但当显示所有数据时,就会出现此错误
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1626249: `character maps to <undefined>`
UnicodeDecodeError:'charmap'编解码器无法解码位置1626249:`字符映射到的字节0x9d`
每次打开文件时都给出编码参数是合理的。使用以下命令:
import json
with open("./Files/CD_Data.json", encoding="utf8") as file:
#item = input("Enter item name: ")
reader = json.load(file)
print(reader)
可能是
json.load(file.read())
或json.load(file)
?检查.load()
和.load()
之间的差异,可能是重复的,或者它不知道如何解释某些字符。尝试设置encoding=“utf8”