Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zlib tutorials
#1
So just started playing with python, and hit a wall with zlib, just running through tutorials and came across this
 import zlib

MESSAGE = "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)
Now when I run it I get this error

Error:
pythonTest.py Traceback(mostrecentcalllast): File"Test.py",line5,in<module> compressed_message=zlib.compress(MESSAGE) TypeError:abytes-likeobjectisrequired,not'str'
I’ve tried a few different tutorials and all seem to give the same error, I’ve tried on python 2.7 and then installed 3.9 still the same
I’ve tried on a different computer too
Have I done something wrong, the tutorials done tell you to convert to binary, plus if you do it puts a “b” in front of the text, also tried ‘ instead of “ as some tutorials use that instead

Bit confused

John
Reply
#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
#3
(Mar-13-2021, 08:35 PM)snippsat Wrote: 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'
Ok, excellent, thank you very much, I’ll get rid of python 2.7 then to save confusion, and only try to find tutorials for 3.9,
Reply


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