Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中读取JSON文件时修复UnicodeDecodeError_Python_Json - Fatal编程技术网

如何在python中读取JSON文件时修复UnicodeDecodeError

如何在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

我正在阅读python中的一个json文件,其中包含大量的进口商品数据以及它们的名称和税款。我想输入一个项目名称并搜索该文件以获取该项目的税款

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”