让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)
但这感觉很糟糕...
but this feels bad...
有没有更好的方法可以将字节文件对象转换为字符串文件对象?还是我缺少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 提供有关情况的概述.
Dive Into Python 3 provides an overview about the situation.
您的变通方法"很好-尽管感觉不对,但这是正确的方法.
Your "work-around" is fine—although it feels wrong, it's the correct way to do it.
这篇关于让JSON对象接受字节或让urlopen输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!