Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Unable to decode response object

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

text has this data in it, b'\x1f\x8b\x08\x00\x00'

like image 946
suhas reddy Avatar asked Oct 31 '25 09:10

suhas reddy


1 Answers

0x1f 0x8b is the starting bytes for gzip files. .decode defaults to utf-8 which does not expect that start byte sequence - hence the invalid start byte message.

Try using the gzip module to decompress the file and then attempt to decode it (provided that the results are actually decodable). Example from the documentation, adapted for your case:

import gzip
data = gzip.decompress(response.read())
text = data.decode('utf-8')
like image 197
Steen Avatar answered Nov 02 '25 00:11

Steen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!