如何从“类似文件的对象”创建 GzipFile 实例? urllib.urlopen() 返回吗?

发布于 2024-10-03 10:44:10 字数 667 浏览 12 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-10-10 10:44:10

urlopen 文档 列出了返回的对象支持的方法。我建议将该对象包装在另一个支持 gzip 所需方法的类中。

其他选项:调用响应对象的 read 方法并将结果放入 StringIO 对象(它应该支持 gzip 期望的所有方法)。不过这可能会贵一点。

例如

import gzip
import json
import StringIO
import urllib

url = urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')
url_f = StringIO.StringIO(url.read())
g = gzip.GzipFile(fileobj=url_f)
j = json.load(g)

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.

import gzip
import json
import StringIO
import urllib

url = urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')
url_f = StringIO.StringIO(url.read())
g = gzip.GzipFile(fileobj=url_f)
j = json.load(g)
浅紫色的梦幻 2024-10-10 10:44:10
import urllib2
import json
import gzip
import io

url='http://api.stackoverflow.com/1.0/badges/name'
page=urllib2.urlopen(url)
gzip_filehandle=gzip.GzipFile(fileobj=io.BytesIO(page.read()))
json_data=json.loads(gzip_filehandle.read())
print(json_data)

io.BytesIO 适用于 Python2.6+。对于旧版本的 Python,您可以使用 cStringIO.StringIO。

import urllib2
import json
import gzip
import io

url='http://api.stackoverflow.com/1.0/badges/name'
page=urllib2.urlopen(url)
gzip_filehandle=gzip.GzipFile(fileobj=io.BytesIO(page.read()))
json_data=json.loads(gzip_filehandle.read())
print(json_data)

io.BytesIO is for Python2.6+. For older versions of Python, you could use cStringIO.StringIO.

彼岸花ソ最美的依靠 2024-10-10 10:44:10

这是 @stefanw 答案的新更新,对于他来说,使用这么多内存可能太昂贵了。

感谢这篇文章(https://www.enricozini.org/blog/ 2011/cazzeggio/python-gzip/,它解释了为什么gzip不起作用),解决方案是使用Python3。

import urllib.request
import gzip

response = urllib.request.urlopen('http://api.stackoverflow.com/1.0/badges/name')
with gzip.GzipFile(fileobj=response) as f:
    for line in f:
        print(line)

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.

import urllib.request
import gzip

response = urllib.request.urlopen('http://api.stackoverflow.com/1.0/badges/name')
with gzip.GzipFile(fileobj=response) as f:
    for line in f:
        print(line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文