Gzip in Python: Difference between revisions
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
* [[Python_Code_Examples#Code_Examples|Python Code Examples]] | * [[Python_Code_Examples#Code_Examples|Python Code Examples]] | ||
=Overview= | =Overview= | ||
=Compress= | |||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
import gzip | import gzip | ||
yaml = ... | yaml = ... | ||
compressed = gzip.compress(bytes(yaml), 'utf-8') | compressed = gzip.compress(bytes(yaml), 'utf-8') | ||
</syntaxhighlight> | |||
=Uncompress= | |||
<syntaxhighlight lang='py'> | |||
import gzip | |||
binary_data = | |||
uncompressed_binary = gzip.decompress(binary_data) | |||
# | |||
# if data is compressed text | |||
# | |||
uncompressed_text = gzip.decompress(binary_data).decode('utf-8') | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 05:36, 10 June 2022
Internal
Overview
Compress
import gzip
yaml = ...
compressed = gzip.compress(bytes(yaml), 'utf-8')
Uncompress
import gzip
binary_data =
uncompressed_binary = gzip.decompress(binary_data)
#
# if data is compressed text
#
uncompressed_text = gzip.decompress(binary_data).decode('utf-8')