Python gzip: is there a way to decompress from a string? Python gzip: is there a way to decompress from a string? python python

Python gzip: is there a way to decompress from a string?


If your data is already in a string, try zlib, which claims to be fully gzip compatible:

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

Read more: http://docs.python.org/library/zlib.html


gzip.open is a shorthand for opening a file, what you want is gzip.GzipFile which you can pass a fileobj

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

vs

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.

so this should work for you

gzip_file_handle = gzip.GzipFile(fileobj=url_file_handle)


You can use gzip.decompress from the gzip builtin Python library(available for Python 3.2+).

Example on how to decompress bytes:

import gzipgzip.decompress(gzip_data)

Documentation

https://docs.python.org/3.5/library/gzip.html#gzip.decompress