简体   繁体   English

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

[英]How to fix UnicodeDecodeError while reading JSON file in python

I'm reading a json file in python which has huge amount of imported goods data with their names and taxes.我正在用 python 读取一个 json 文件,其中包含大量带有名称和税费的进口商品数据。 I want to input an item name and search through that file to get tax of that item.我想输入一个项目名称并搜索该文件以获取该项目的税款。

import json

with open("./Files/CD_Data.json") as file:
    #item = input("Enter item name: ")
    reader = json.load(file)
    print(reader)

But when display all data then this error comes up.但是当显示所有数据时,就会出现这个错误。

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1626249: `character maps to <undefined>`

It is reasonable to give encoding parameter every time you open a file.每次打开文件时都给出编码参数是合理的。 Use this:用这个:

import json

with open("./Files/CD_Data.json", encoding="utf8") as file:
    #item = input("Enter item name: ")
    reader = json.load(file)
    print(reader)

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

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