将 python 字节类型更改为字符串
我正在使用 python 来使用 stackoverflow API。我运行以下命令:
f = urllib.request.urlopen('http://api.stackoverflow.com/1.0/stats')
d = f.read()
d 的类型是 'bytes' 类,如果我打印它,它看起来像:
b'\x1f\x8b\x08\x00\x00\x00 .... etc
我尝试 d=f.read().decode('utf-8')
作为这是标题中指示的字符集,但我得到了 'utf8' 编解码器无法解码位置 1 中的字节 0x8b"
错误消息
如何将从 urllib.request 调用中收到的字节对象转换为字符串?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查以确保您的响应正文未经过 gzip 压缩。相信其传输编码或响应头等,我非常有信心您处理压缩数据而不是字符集编码问题。
更新:意识到我有一个不解释/提供足够细节的坏习惯。对于 Python gzip'd 字节字符串,它们总是以 1f8b 开头 有人在这里更好地解释了 https://stackoverflow.com/a/3703300/9908
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