python: read lines from compressed text files python: read lines from compressed text files python python

python: read lines from compressed text files


Using gzip.GzipFile:

import gzipwith gzip.open('input.gz','rt') as f:    for line in f:        print('got line', line)

Note: gzip.open(filename, mode) is an alias for gzip.GzipFile(filename, mode).I prefer the former, as it looks similar to with open(...) as f: used for opening uncompressed files.


You could use the standard gzip module in python. Just use:

gzip.open('myfile.gz')

to open the file as any other file and read its lines.

More information here: Python gzip module


Have you tried using gzip.GzipFile? Arguments are similar to open.