【问题标题】:UnicodeDecodeError when import json file导入 json 文件时出现 UnicodeDecodeError
【发布时间】:2023-03-24 21:08:01
【问题描述】:

我想在python中打开一个json文件,但出现错误:

UnicodeDecodeError:“ascii”编解码器无法解码位置 64864 中的字节 0xe2:序数不在范围内 (128)

我的代码很简单:

# -*- coding: utf-8 -*-

import json

with open('birdw3l2.json') as data_file:    
    data = json.load(data_file)
print(data)

有人可以帮助我吗?谢谢!

【问题讨论】:

    标签: python json


    【解决方案1】:

    您应该在加载 json 文件时指定编码格式。像这样:

     data = json.load(data_file, encoding='utf-8')
    

    编码取决于您的文件编码。

    【讨论】:

    【解决方案2】:

    试试下面的代码。

    import json
    
    with open('birdw3l2.json') as data_file:    
        data = json.load(data_file).decode('utf-8')
    print(data)
    

    【讨论】:

    • 不会改变任何事情:(
    • 不,总是同样的问题。实际上,即使我不打印,错误仍然存​​在......
    • 实际上您的文件包含一些特殊字符。如果是小文件,再写一遍
    • 或者写一个程序先去掉那个特殊字符
    • 这是一个相当大的数据文件
    猜你喜欢
    • 2017-01-05
    • 1970-01-01
    • 2012-09-19
    • 2018-12-26
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多