Last active
August 29, 2015 14:03
-
-
Save alexland/f07e90e8ffdd380e6d2d to your computer and use it in GitHub Desktop.
compressed file IO in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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