Python 请求返回字节,我无法解码它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31708519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request returns bytes and I'm failing to decode them
提问by koda gates
Essentially I made a request to a website and got a byte response back: b'[{"geonameId:"703448"}..........'.
I'm confused because although it is of type byte, it is very human readable and appears like a list of json. I do know that the response is encoded in latin1 from running r.encoding
which returned ISO-859-1
and I have tried to decode it, but it just returns an empty string. Here's what I have so far:
本质上,我向网站发出请求并得到了一个字节响应:b'[{"geonameId:"703448"}..........'.
我很困惑,因为虽然它是字节类型,但它非常易读并且看起来像一个 json 列表。我确实知道响应是从运行r.encoding
返回的latin1 中编码的ISO-859-1
,我试图对其进行解码,但它只返回一个空字符串。这是我到目前为止所拥有的:
r = response.content
string = r.decode("ISO-8859-1")
print (string)
and this is where it prints a blank line. However when I run
这是它打印一个空行的地方。但是当我跑
len(string)
I get: back 31023
How can I decode these bytes without getting back an empty string?
我得到: back31023
如何在不返回空字符串的情况下解码这些字节?
采纳答案by mzc
Did you try to parse it with the json
module?
您是否尝试使用json
模块解析它?
import json
parsed = json.loads(response.content)
回答by salah
Another solution is to use response.text, which returns the content in unicode
另一种解决方案是使用 response.text,它以 unicode 格式返回内容
Type: property
String form: <property object at 0x7f76f8c79db8>
Docstring:
Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
``chardet``.
The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.
回答by Martin Thoma
There is r.text
and r.content
. The first one is a string, the second one is bytes.
有r.text
和r.content
。第一个是字符串,第二个是字节。
You want
你要
import json
data = json.loads(r.text)