Python json.dump - UnicodeDecodeError: 'utf8' 编解码器无法解码位置 0 中的字节 0xbf:起始字节无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25122371/
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
json.dump - UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte
提问by Belphegor
I have a dictionary datawhere I have stored:
我有一本data存储字典:
key- ID of an eventvalue- the name of this event, wherevalueis a UTF-8 string
key- 事件IDvalue- 此事件的名称,其中value是 UTF-8 字符串
Now, I want to write down this map into a json file. I tried with this:
现在,我想把这张地图写成一个 json 文件。我试过这个:
with open('events_map.json', 'w') as out_file:
json.dump(data, out_file, indent = 4)
but this gives me the error:
但这给了我错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte
UnicodeDecodeError: 'utf8' 编解码器无法解码位置 0 中的字节 0xbf:起始字节无效
Now, I also tried with:
现在,我也尝试过:
with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
out_file.write(unicode(json.dumps(data, encoding="utf-8")))
but this raises the same error:
但这会引发相同的错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte
UnicodeDecodeError: 'utf8' 编解码器无法解码位置 0 中的字节 0xbf:起始字节无效
I also tried with:
我也试过:
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)))
but this raises the error:
但这会引发错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 3114: ordinal not in range(128)
UnicodeDecodeError: 'ascii' 编解码器无法解码位置 3114 中的字节 0xbf:序号不在范围内 (128)
Any suggestions about how can I solve this problem?
有关如何解决此问题的任何建议?
EDIT:I believe this is the line that is causing me the problem:
编辑:我相信这是导致我出现问题的线路:
> data['142']
'\xbf/ANCT25'
EDIT 2:The datavariable is read from a file. So, after reading it from a file:
编辑2:该data变量被从文件中读取。因此,从文件中读取后:
data_file_lines = io.open(file_name, 'r', encoding='utf8').readlines()
I then do:
然后我做:
with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
json.dump(data, json_file, ensure_ascii=False)
Which gives me the error:
这给了我错误:
TypeError: must be unicode, not str
类型错误:必须是 unicode,而不是 str
Then, I try to do this with the data dictionary:
然后,我尝试使用数据字典执行此操作:
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')
which is, again, followed by:
其次是:
with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
json.dump(data, json_file, ensure_ascii=False)
but again, the same error:
但同样,同样的错误:
TypeError: must be unicode, not str
I get the same error when I use the simple openfunction for reading from the file:
当我使用简单open函数从文件中读取时,我遇到了同样的错误:
data_file_lines = open(file_name, "r").readlines()
采纳答案by Martijn Pieters
The exception is caused by the contents of your datadictionary, at least oneof the keys or values is notUTF-8 encoded.
异常是由data字典的内容引起的,至少有一个键或值不是UTF-8 编码的。
You'll have to replace this value; either by substituting a value that isUTF-8 encoded, or by decoding it to a unicodeobject by decoding just that value with whatever encoding is the correct encoding for that value:
你必须替换这个值;或者通过用一个值,该值是UTF-8编码的,或者通过将其进行解码,以一个unicode由解码只是与任何编码是该值的正确的编码值对象:
data['142'] = data['142'].decode('latin-1')
to decode that string as a Latin-1-encoded value instead.
将该字符串解码为 Latin-1-encoded 值。

