请求返回字节,我无法解码它们 [英] Request returns bytes and I'm failing to decode them
问题描述
基本上我向网站提出请求并得到一个字节响应:b'[{"geonameId:"703448"}..........
I'我很困惑,因为虽然它是字节类型,但它非常易读,看起来像一个 json 列表.我知道响应是通过运行 r.encoding
以 latin1 编码的,它返回了 ISO-859-1
并且我尝试对其进行解码,但它只返回一个空字符串.这是我目前所拥有的:
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:
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)
我得到:返回 31023
如何在不返回空字符串的情况下解码这些字节?
I get: back 31023
How can I decode these bytes without getting back an empty string?
推荐答案
您是否尝试使用 json
模块解析它?
Did you try to parse it with the json
module?
import json
parsed = json.loads(response.content)
这篇关于请求返回字节,我无法解码它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!