如何从“类似文件的对象”创建 GzipFile 实例? urllib.urlopen() 返回吗?
我正在使用 Python 玩弄 Stack Overflow API。我正在尝试解码 API 提供的压缩响应。
import urllib, gzip
url = urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')
gzip.GzipFile(fileobj=url).read()
根据urllib2文档,urlopen
“返回一个文件-像物体”。
但是,当我在使用它创建的 GzipFile 对象上运行 read()
时,出现此错误:
AttributeError: addinfourl instance has no attribute 'tell'
据我所知,这是来自 urlopen 返回的对象
。
它似乎也没有进行查找,因为当我这样做时出现错误:
url.read()
url.seek(0)
这个对象到底是什么,以及如何从中创建一个功能正常的 GzipFile 实例?
I’m playing around with the Stack Overflow API using Python. I’m trying to decode the gzipped responses that the API gives.
import urllib, gzip
url = urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')
gzip.GzipFile(fileobj=url).read()
According to the urllib2 documentation, urlopen
“returns a file-like object”.
However, when I run read()
on the GzipFile object I’ve created using it, I get this error:
AttributeError: addinfourl instance has no attribute 'tell'
As far as I can tell, this is coming from the object returned by urlopen
.
It doesn’t appear to have seek either, as I get an error when I do this:
url.read()
url.seek(0)
What exactly is this object, and how do I create a functioning GzipFile
instance from it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
urlopen 文档 列出了返回的对象支持的方法。我建议将该对象包装在另一个支持 gzip 所需方法的类中。
其他选项:调用响应对象的 read 方法并将结果放入 StringIO 对象(它应该支持 gzip 期望的所有方法)。不过这可能会贵一点。
例如
The urlopen docs list the supported methods of the object that is returned. I recommend wrapping the object in another class that supports the methods that gzip expects.
Other option: call the read method of the response object and put the result in a StringIO object (which should support all methods that gzip expects). This maybe a little more expensive though.
E.g.
io.BytesIO 适用于 Python2.6+。对于旧版本的 Python,您可以使用 cStringIO.StringIO。
io.BytesIO
is for Python2.6+. For older versions of Python, you could usecStringIO.StringIO
.这是 @stefanw 答案的新更新,对于他来说,使用这么多内存可能太昂贵了。
感谢这篇文章(https://www.enricozini.org/blog/ 2011/cazzeggio/python-gzip/,它解释了为什么
gzip
不起作用),解决方案是使用Python3。Here is a new update for @stefanw's answer, to whom that might think it too expensive to use that much memory.
Thanks to this article(https://www.enricozini.org/blog/2011/cazzeggio/python-gzip/, it explains why
gzip
doesn't work), the solution is to use Python3.