Change python byte type to string

自古美人都是妖i 提交于 2019-12-07 05:59:23

问题


I'm using python to play with the stackoverflow API. I run the following commands:

f = urllib.request.urlopen('http://api.stackoverflow.com/1.0/stats')
d = f.read()

The type of d is class 'bytes' and if I print it it looks like:

b'\x1f\x8b\x08\x00\x00\x00 .... etc

I tried d=f.read().decode('utf-8') as that is the charset indicated in the header, but I get a 'utf8' codec can't decode byte 0x8b in position 1" error message

How do I convert the byte object I received from my urllib.request call to a string?


回答1:


Check to make sure your response body is not gzipped. Believe its transfer encoding or such for the response header, i have a high confidence that your dealing with compressed data and not character set encoding issues.

update: Realizing I have a bad habit of not explaining/providing enough detail. For Python gzip'd byte strings they always start with 1f8b Someone explains it better here https://stackoverflow.com/a/3703300/9908



来源:https://stackoverflow.com/questions/3746993/change-python-byte-type-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!