-1

This question is tacked several times here and here but not answered completely. As decompression doesn't yield the original string.

>>> s = "some string to test zlib"
>>> z = zlib.compress(s)
Traceback (most recent call last):
   File "<input>", line 1, in <module>
   TypeError: a bytes-like object is required, not 'str'
>>> z = zlib.compress(s.encode("utf8"))
>>> y = zlib.decompress(z)
>>> y
b'some string to test zlib'
>>> print (s, y)
some string to test zlib b'some string to test zlib'

So decompression returns the original string included in "b''"

>>> p = str(y)
>>> p
"b'some string to test zlib'"

The same behavior by the way with gzip.

user1491229
  • 683
  • 1
  • 9
  • 14

1 Answers1

2

y.decode('utf-8') - the b is just an indication that this is a bytes type being printed:

s = "some string to test zlib"
z = zlib.compress(s.encode("utf-8"))
y = zlib.decompress(z)
y
print (s, y)
print (s, y.decode('utf-8'))

Output:

some string to test zlib b'some string to test zlib'
some string to test zlib some string to test zlib
Or Y
  • 2,088
  • 3
  • 16