json.dump - UnicodeDecodeError:'utf8'编解码器无法解码位置0的0xbf字节:无效的起始字节 [英] json.dump - UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte

查看:4263
本文介绍了json.dump - UnicodeDecodeError:'utf8'编解码器无法解码位置0的0xbf字节:无效的起始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典 data 我已经存储的地方:




  • key - 活动ID =


  • 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')作为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')作为out_file:
out_file.write(unicode(json.dumps (data,encoding =utf-8,ensure_ascii = False))

但这会引发错误:


UnicodeDecodeError:'ascii'编解码器无法解码位置3114的字节0xbf:序号不在范围(128)


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



编辑:
我相信这是导致我问题的一行:

 >数据['142'] 
'\xbf / ANCT25'

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

  data_file_lines = io.open(file_name,'r',encoding ='utf8' ).readlines()

然后我做:



io.open('data / events_map.json','w',encoding ='utf8')作为json_file:
json.dump(data,json_file,确保_ascii = False)

哪个给我错误:


TypeError:必须是unicode,而不是str


然后,我尝试这样做数据字典:

  for sortedupuple中的元组(`data`变量由元组初始化):
数据[str(tuple [1])] = json.dumps(tuple [0],ensure_ascii = False,encoding ='utf8')

再次跟着:

 与io.open('data / events_map。 json','w',encoding ='utf8')as json_file:
json.dump(data,json_file,ensure_ascii = Fa lse)

但同样的错误:

  TypeError:必须是unicode,而不是str 

I当我使用简单的打开函数从文件中读取时,得到相同的错误:

  data_file_lines = open(file_name,r)readlines()


解决方案

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



您必须替换此值;或者通过将UTF-8编码的值替换为 UTF-8,或者将其解码为 unicode 对象,通过用任何编码解码该值,正确编码该值:

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

将该字符串解码为拉丁文1编码的值。


I have a dictionary data where I have stored:

  • key - ID of an event

  • value - the name of this event, where value is a UTF-8 string

Now, I want to write down this map into a json file. I tried with this:

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

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

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)

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 data variable is read from a file. So, after reading it from a file:

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

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 open function for reading from the file:

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

解决方案

The exception is caused by the contents of your data dictionary, at least one of the keys or values is not UTF-8 encoded.

You'll have to replace this value; either by substituting a value that is UTF-8 encoded, or by decoding it to a unicode object by decoding just that value with whatever encoding is the correct encoding for that value:

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

to decode that string as a Latin-1-encoded value instead.

这篇关于json.dump - UnicodeDecodeError:'utf8'编解码器无法解码位置0的0xbf字节:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆