我正在使用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' codec can't decode byte 0x8b in position 1"
我该如何将从urllib.request调用中接收到的字节对象转换为字符串?