pandas Python - UnicodeDecodeError:'charmap' 编解码器无法解码位置 44 中的字节 0x81:字符映射到 <undefined>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43152368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:19:27  来源:igfitidea点击:

Python - UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 44: character maps to <undefined>

jsonpython-3.xpandasjupyter-notebook

提问by userPyGeo

Using pandas on Python 3 Jupyter notebook, I got

在 Python 3 Jupyter notebook 上使用 pandas,我得到了

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 44: character maps to

UnicodeDecodeError: 'charmap' 编解码器无法解码位置 44 中的字节 0x81:字符映射到

error while trying to read a json file that looks like this:

尝试读取如下所示的 json 文件时出错:

{
    "Test1": {
        "A": "攻撃を続ける",
        "B": "残り資源",
        "C": "残りの資源を得るため小隊を修理し戦闘を続けろ:"
    },
    "Test2": {
        "D": "{x} 日目",
        "E": "CC レベル {x}",
        "F": "本当にこれから全てのデバイスでこの基地を使用しますか?",
        "G": "この{social_network}アカウントには2つの基地が存在してます。基地の数は一人のプレイヤーにつき一つに限定されています。基地を選択するか、キャンセルしてください。",
    }
}

Any idea how to solve this?

知道如何解决这个问题吗?

See full error message here

在此处查看完整的错误消息

import pandas as pd

json_df = pd.read_json('input.json')
json_df

EDIT: I have also tried reading the json with the JSON module, it still the same error.

编辑:我也试过用 JSON 模块读取 json,它仍然是同样的错误。

采纳答案by pts

Your .jsonfile is encoded as UTF-8. pd.read_jsontries to decode it as CP1252. You need to make it decode it as UTF-8:

您的.json文件编码为 UTF-8。pd.read_json尝试将其解码为 CP1252。您需要将其解码为 UTF-8:

import pandas as pd

json_df = pd.read_json('input.json', encoding='UTF-8')
json_df