Python gzip: есть ли способ распаковать из строки? - PullRequest
28 голосов
/ 09 октября 2009

Я прочитал это ТАК сообщение вокруг проблемы безрезультатно.

Я пытаюсь распаковать файл .gz с URL.

url_file_handle=StringIO( gz_data )
gzip_file_handle=gzip.open(url_file_handle,"r")
decompressed_data = gzip_file_handle.read()
gzip_file_handle.close()

... но я получаю TypeError: приведение к Unicode: нужна строка или буфер, cStringIO.StringI найдено

Что происходит?

Traceback (most recent call last):  
  File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2974, in _HandleRequest
    base_env_dict=env_dict)
  File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 411, in Dispatch
    base_env_dict=base_env_dict)
  File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2243, in Dispatch
    self._module_dict)
  File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2161, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2057, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "/home/jldupont/workspace/jldupont/trunk/site/app/server/tasks/debian/repo_fetcher.py", line 36, in <module>
    main()
  File "/home/jldupont/workspace/jldupont/trunk/site/app/server/tasks/debian/repo_fetcher.py", line 30, in main
    gziph=gzip.open(fh,'r')
  File "/usr/lib/python2.5/gzip.py", line 49, in open
    return GzipFile(filename, mode, compresslevel)
  File "/usr/lib/python2.5/gzip.py", line 95, in __init__
    fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
TypeError: coercing to Unicode: need string or buffer, cStringIO.StringI found

Ответы [ 3 ]

45 голосов
/ 19 августа 2013

Если ваши данные уже находятся в строке, попробуйте zlib, который утверждает, что он полностью совместим с gzip:

import zlib
decompressed_data = zlib.decompress(gz_data, 16+zlib.MAX_WBITS)

Подробнее: http://docs.python.org/library/zlib.html

35 голосов
/ 09 октября 2009

gzip.open - это сокращение для открытия файла. gzip.GzipFile, который вы можете передать fileobj

open(filename, mode='rb', compresslevel=9)
    #Shorthand for GzipFile(filename, mode, compresslevel).

против

class GzipFile
   __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None)
   #    At least one of fileobj and filename must be given a non-trivial value.

так что это должно работать для вас

gzip_file_handle = gzip.GzipFile(fileobj=url_file_handle)
1 голос
/ 20 января 2017

Попробуйте использовать gzip.GzipFile, если вам не нравится передавать непонятные аргументы zlib.decompress.

Когда вы имеете дело с urllib2.urlopen ответом, который может быть сжат или сжат gzip:

import gzip
from StringIO import StringIO

# response = urllib2.urlopen(...

content_raw = response.read()
if 'gzip' in response.info().getheader('Content-Encoding'):
    content = gzip.GzipFile(fileobj=StringIO(content_raw)).read()

Когда вы работаете с файлом, который может хранить сжатые или несжатые данные:

import gzip

# some_file = open(...

try:
    content = gzip.GzipFile(fileobj=some_file).read()
except IOError:
    some_file.seek(0)
    content = some_file.read()

Примеры выше в Python 2.7

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...