Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zlib tutorials
#2
Code is for Python 2 which is dead💀
If change code and run with Python 3.9.
import zlib

MESSAGE = b"life of brian"

compressed_message = zlib.compress(MESSAGE)
decompressed_message = zlib.decompress(compressed_message)

print("original:", repr(MESSAGE))
print("compressed message:", repr(compressed_message))
print("decompressed message:", repr(decompressed_message))
Output:
original: b'life of brian' compressed message: b'x\x9c\xcb\xc9LKU\xc8OSH*\xcaL\xcc\x03\x00!\x08\x04\xc2' decompressed message: b'life of brian'
So it need to be bytes in and it give bytes out.
You can easy change later if need string.
>>> decompressed_message
b'life of brian'
>>> decompressed_message.decode()
'life of brian'

# Other way around
>>> s = 'life of brian'
>>> s
'life of brian'
>>> s.encode()
b'life of brian'
Reply


Messages In This Thread
Zlib tutorials - by Vwjap - Mar-13-2021, 06:57 PM
RE: Zlib tutorials - by snippsat - Mar-13-2021, 08:35 PM
RE: Zlib tutorials - by Vwjap - Mar-13-2021, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 9,463 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  Hi, looking for "dunder" tutorials. AlluminumFoil 3 2,867 Mar-02-2020, 08:37 PM
Last Post: jefsummers
  I do not understand why my python looks different from tutorials. noodlespinbot 2 7,064 Oct-12-2019, 09:56 PM
Last Post: noodlespinbot
  Python Modules and Tutorials marienbad 1 2,787 Jan-31-2019, 08:36 PM
Last Post: ichabod801
  mpi4py examples/tutorials MuntyScruntfundle 1 3,420 Dec-01-2018, 10:22 PM
Last Post: Larz60+
  How yo find "zlib" necessary for "pyenv" in Linux sylas 1 4,161 Aug-13-2018, 10:27 AM
Last Post: Larz60+
  Use of zlib and iconv in building lxml sachinsharma 3 5,192 Jan-16-2018, 09:12 AM
Last Post: sachinsharma
  in tutorials: Never use "for i in range(len(sequence)): Skaperen 1 4,817 Oct-07-2016, 05:05 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020