将 python 字节类型更改为字符串

发布于 2024-09-24 12:52:27 字数 430 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-10-01 12:52:27

检查以确保您的响应正文未经过 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文