让 JSON 对象接受字节或让 urlopen 输出字符串 [英] Let JSON object accept bytes or let urlopen output strings
问题描述
使用 Python 3,我从 URL 请求一个 json 文档.
With Python 3 I am requesting a json document from a URL.
response = urllib.request.urlopen(request)
response
对象是一个类似文件的对象,带有 read
和 readline
方法.通常可以使用以文本模式打开的文件来创建 JSON 对象.
The response
object is a file-like object with read
and readline
methods. Normally a JSON object can be created with a file opened in text mode.
obj = json.load(fp)
我想做的是:
obj = json.load(response)
但是这不起作用,因为 urlopen 以二进制模式返回文件对象.
This however does not work as urlopen returns a file object in binary mode.
解决方法当然是:
str_response = response.read().decode('utf-8')
obj = json.loads(str_response)
但这感觉很糟糕...
有没有更好的方法可以将字节文件对象转换为字符串文件对象?或者我是否缺少 urlopen
或 json.load
的任何参数来提供编码?
Is there a better way that I can transform a bytes file object to a string file object? Or am I missing any parameters for either urlopen
or json.load
to give an encoding?
推荐答案
HTTP 发送字节.如果相关资源是文本,则字符编码通常由 Content-Type HTTP 标头或其他机制(RFC、HTML meta http-equiv
、...)指定.
HTTP sends bytes. If the resource in question is text, the character encoding is normally specified, either by the Content-Type HTTP header or by another mechanism (an RFC, HTML meta http-equiv
,...).
urllib
应该知道如何将字节编码为字符串,但它太天真了——它是一个可怕的功能不足且非 Pythonic 的库.
urllib
should know how to encode the bytes to a string, but it's too naïve—it's a horribly underpowered and un-Pythonic library.
深入 Python 3提供有关情况的概述.
你的变通办法"很好——虽然感觉不对,但这是正确的做法.
Your "work-around" is fine—although it feels wrong, it's the correct way to do it.
这篇关于让 JSON 对象接受字节或让 urlopen 输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!