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

2024-05-15 00:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

import json

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

但是当显示所有数据时,就会出现这个错误。在

^{pr2}$

Tags: 文件数据import目的名称jsonwithfiles
1条回答
网友
1楼 · 发布于 2024-05-15 00:24:26

每次打开一个文件时都给出编码参数是合理的。使用这个:

import json

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

相关问题 更多 >

    热门问题