簡體   English   中英

`UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte`

[英]`UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte`

無法解碼響應 object

 text = response.read()
 if isinstance(text, bytes):  
    text = text.decode('utf-8')  

text里面有這個數據,b'\x1f\x8b\x08\x00\x00'

0x1f 0x8b是 gzip 文件的起始字節.decode默認為utf-8不期望起始字節序列 - 因此invalid start byte消息。

嘗試使用gzip模塊解壓縮文件,然后嘗試對其進行解碼(前提是結果實際上是可解碼的)。 文檔中的示例,適用於您的案例:

import gzip
data = gzip.decompress(response.read())
text = data.decode('utf-8')

我有一個類似的問題,我只是沒有注意到我的文件實際上是壓縮的。 即使擴展名不存在,您的文件也可能被壓縮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM