问题描述
使用Python3我从一些url请求一个json文档。
With Python3 I am requesting from some url a json document.
response = urllib.request.urlopen(request)
响应
对象是一个像读,读线功能。
The response
object is a file like object with read, readline functions.
通常可以使用一个文件(以textmode打开)创建一个json对象
Normally a json object can be created with a file (opened in textmode)
obj = json.load(fp)
我想做的是: / p>
What I would like to do is:
obj = json.load(response)
然而,这个urlopen以二进制模式返回文件对象不起作用。
this however does not work as urlopen returns a file object in binary mode.
当然有一个工作:
str_response = response.readall().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 byte file object to a string file object? Or am I missing any parameters for either urlopen
or json.load
to give an encoding?
这将会是一个常见的用例,所以我有信心丢失一些有用的函数。
This would look to me as a common use case so I'm confident I'm missing some usefull function.
推荐答案
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.
提供了有关情况的概述。
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.
这篇关于Python 3,让json对象接受字节或让urlopen输出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!