Skip to content

Instantly share code, notes, and snippets.

@alexland
Last active August 29, 2015 14:03
Show Gist options
  • Save alexland/f07e90e8ffdd380e6d2d to your computer and use it in GitHub Desktop.
Save alexland/f07e90e8ffdd380e6d2d to your computer and use it in GitHub Desktop.
compressed file IO in python
df = '~/path/to/some/text/file.csv'
df1 = 'path/to/some/text/file.gz'
import zlib
import gzip
with open(df, 'r', encoding='utf-8') as fh:
tx = fh.read()
# compress a text file
with gzip.open(df1, 'wt') as fh:
fh.write(tx)
# read a compressed text file line-by-line
with gzip.open(df1, 'rt') as fh:
tx = (line.strip().split(',') for line in fh.readlines())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment