简体   繁体   English

UnicodeDecodeError:“ utf-8”编解码器无法解码位置127的字节0xd0:数据意外结束

[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 127: unexpected end of data

I have a problem decoding some characters, the error is like this: 我在解码某些字符时遇到问题,错误是这样的:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 127: unexpected end of data

Below is my code, 'response' variable is JSON 下面是我的代码,“ response”变量是JSON

response = requests.post('LINK-TO-API', headers=headers, data=data)
result = ""
for i in response:
    result += i.decode('utf-8')

whats wrong with my code? 我的代码有什么问题? Thanks 谢谢

0xD0 ( 0b11010000 ) is one of many bytes that indicate the start of a multi-byte sequence in UTF-8. 0xD00b11010000 )是表示多字节序列的UTF-8的开始的字节数中的一个。 The number of 1s before the first 0 indicate the length of the sequence*. 第一个0之前的1表示序列的长度*。 The bits after the first 0 are part of the encoding of the code point. 第一个0之后的位是代码点编码的一部分。

Basically, the iterator of the response has cut a two byte encoding in half. 基本上,响应的迭代器将两个字节的编码减少了一半。 You should read the entire contents of the response before trying to decode it. 您应先读取响应的全部内容,然后再尝试对其进行解码。 eg. 例如。

bytes_ = b''
for chunk in response:
    bytes_ += chunk
result = bytes_.decode('utf8')

* bytes starting 10 indicate a continuation byte in a multi-byte sequence rather than a 1-byte encoding. *以10开头的字节表示多字节序列中的连续字节,而不是1字节编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 渲染时捕获UnicodeDecodeError:'utf8'编解码器无法解码位置0中的字节0xd0:意外的数据结束 - Caught UnicodeDecodeError while rendering: 'utf8' codec can't decode byte 0xd0 in position 0: unexpected end of data UnicodeDecodeError:“ utf-8”编解码器无法解码位置65535中的字节0xd9:数据意外结束 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd9 in position 65535: unexpected end of data Pandas read_excel UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: 无效的继续字节 - Pandas read_excel UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 1023 中的字节 0xe2:数据意外结束 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data Python UnicodeDecodeError:'ascii'编解码器无法解码位置12的字节0xd0:序数不在范围内(128) - Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128) Python:UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xd0:序数不在范围内(128) - Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128) 'utf8'编解码器无法解码位置0的字节0xd0:无效的连续字节 - 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte UnicodeDecodeError:'utf-8'编解码器无法解码位置65534-65535中的字节:意外的数据结束 - UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 65534-65535: unexpected end of data UnicodeDecodeError: 'utf8' 编解码器无法解码位置 34 中的字节 0xc3:数据意外结束 - UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 34: unexpected end of data UnicodeDecodeError:'utf8'编解码器无法解码位置0的字节0xc3:意外的数据结束 - UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM