json.dump - UnicodeDecodeError:' utf8'编解码器不能解码位置0中的字节0xbf:无效的起始字节

时间:2014-08-04 15:34:18

标签: python json unicode encoding utf-8

我有一个字典data,我已存储:

  • key - 活动ID

  • value - 此事件的名称,其中value是UTF-8字符串

现在,我想将此地图写入json文件。我试过这个:

with open('events_map.json', 'w') as out_file:
    json.dump(data, out_file, indent = 4)

但这给了我错误:

  

UnicodeDecodeError:' utf8'编解码器不能解码位置0的字节0xbf:   起始字节无效

现在,我也尝试过:

with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
   out_file.write(unicode(json.dumps(data, encoding="utf-8")))

但这引发了同样的错误:

  

UnicodeDecodeError:' utf8'编解码器不能解码位置0的字节0xbf:   起始字节无效

我也尝试过:

with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
    out_file.write(unicode(json.dumps(data, encoding="utf-8", ensure_ascii=False)))

但这会引发错误:

  

UnicodeDecodeError:' ascii'编解码器不能解码位置3114中的字节0xbf:序数不在范围内(128)

有关如何解决此问题的任何建议?

修改 我相信这是导致我这个问题的路线:

> data['142']
'\xbf/ANCT25'

编辑2: 从文件中读取data变量。所以,从文件中读取后:

data_file_lines = io.open(file_name, 'r', encoding='utf8').readlines()
然后我做了:

with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
        json.dump(data, json_file, ensure_ascii=False)

这给了我错误:

  

TypeError:必须是unicode,而不是str

然后,我尝试使用数据字典执行此操作:

for tuple in sorted_tuples (the `data` variable is initialized by a tuple):
    data[str(tuple[1])] = json.dumps(tuple[0], ensure_ascii=False, encoding='utf8')

再次,接着是:

with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
    json.dump(data, json_file, ensure_ascii=False)

但同样的错误:

TypeError: must be unicode, not str

当我使用简单的open函数从文件中读取时,我得到了同样的错误:

data_file_lines = open(file_name, "r").readlines()

1 个答案:

答案 0 :(得分:15)

异常是由data词典的内容引起的,至少一个的键或值 UTF-8编码。

你必须替换这个值;通过替换 UTF-8编码的值,或者通过仅使用任何编码解码该值来将其解码为unicode对象,该值是该值的正确编码:

data['142'] = data['142'].decode('latin-1')

将该字符串解码为Latin-1编码的值。

相关问题