【问题标题】:How to fix UnicodeDecodeError while reading JSON file in python如何在 python 中读取 JSON 文件时修复 UnicodeDecodeError
【发布时间】:2019-03-07 15:16:01
【问题描述】:

我正在使用 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>`

【问题讨论】:

标签: python json


【解决方案1】:

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

import json

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

【讨论】:

  • 使用这种精确的方法获取JSONDecodeError: Extra data。文件加载为TextIOWrapper
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 2013-08-12
相关资源
最近更新 更多