开发者

Python: what is the correct way to handle gzipped json?

I've found this snippet, which seems to do the job, but I can't understand why it uses StringIO. Isn't f already a file-like object? What is the need to read it, then make it look like a file ag开发者_开发百科ain, only to read it again? I've tested it (well, a slightly modified version of it), and it doesn't work without StringIO.


Seems to be a flaw in python standard library which is fixed in Python 3.2.
see http://www.enricozini.org/2011/cazzeggio/python-gzip/

urllib and urllib2 file objects do not provide a method tell() as requested by gzip.


It's possible that the gunzip code needs a file-like object that has a seek method, which a HTTP library is very unlikely to provide. What does "doesn't work" mean? Error message?

If efficiency is your real concern, slightly modify the code so that it uses cStringIO, not StringIO.


The way I read the relevant part of the code says:

  • Open an url
  • Download it completely into memory (with the read method)
  • Store the content in a StringIO object, so that it's available as a file-like object
  • Do the gzip and json stuff with it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜