![](/img/trans.png)
[英]Caught UnicodeDecodeError while rendering: 'utf8' codec can't decode byte 0xd0 in position 0: unexpected end of data
[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 127: unexpected end of data
我在解碼某些字符時遇到問題,錯誤是這樣的:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 127: unexpected end of data
下面是我的代碼,“ response”變量是JSON
response = requests.post('LINK-TO-API', headers=headers, data=data)
result = ""
for i in response:
result += i.decode('utf-8')
我的代碼有什么問題? 謝謝
0xD0
( 0b11010000
)是表示多字節序列的UTF-8的開始的字節數中的一個。 第一個0之前的1表示序列的長度*。 第一個0之后的位是代碼點編碼的一部分。
基本上,響應的迭代器將兩個字節的編碼減少了一半。 您應先讀取響應的全部內容,然后再嘗試對其進行解碼。 例如。
bytes_ = b''
for chunk in response:
bytes_ += chunk
result = bytes_.decode('utf8')
*以10
開頭的字節表示多字節序列中的連續字節,而不是1字節編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.