How do I gzip compress a string in Python?
gzip.GzipFile
exists, but that's for file objects - what about with plain strings?
How do I gzip compress a string in Python?
gzip.GzipFile
exists, but that's for file objects - what about with plain strings?
Pick a suitable module from http://docs.python.org/library/archiving.html -- either gzip or zlib, depending on your exact needs.
For those who want to compress a Pandas dataframe in JSON format:
Tested with Python 3.6 and Pandas 0.23
The easiest way is the
zlib
encoding:Then you decompress it with:
If you want to produce a complete
gzip
-compatible binary string, with the header etc, you could usegzip.GzipFile
together withStringIO
: