简体   繁体   中英

Python zlib to decompress gzipped data

If I have the following data set,

a 10
a 23
a  1
a  1
a  1
b 22
b 33
b  1
b  1

and someone gzips it using gzip in a Unix terminal into data.gz .

How could I use the zlib module to decompress it? I know the gzip module is an option, and it appears that gzip calls zlib, but I would like to know how to do this directly. The reason I am interested is zlib, is that gzip is really slow for the large files I work with. Usually, I use zcat to decompress files and read records using sys.stdin . However, I'm looking for another way.

Have you try that?

Using the example you gave

In [4]: import gzip

In [5]: f = gzip.open('words.gz','rb')

In [6]: file_content = f.read()

In [7]: f.close()

In [8]: print(file_content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM