I don't understand in 2.X it worked :
import zlib
zlib.compress('Hello, world')
now i have a :
zlib.compress("Hello world!")
TypeError: must be bytes or buffer, not str
How can i compress my string ? Regards Bussiere
Start by taking the first character of the given string and appending it to the compressed string. Next, count the number of occurrences of that specific character and append it to the compressed string. Repeat this process for all the characters until the end of the string is reached.
This is meant to enforce that you actually have a defined encoding.
zlib.compress("Hello, world".encode("utf-8"))
b'x\x9c\xf3H\xcd\xc9\xc9\xd7Q(\xcf/\xcaI\x01\x00\x1b\xd4\x04i'
zlib.compress("Hello, world".encode("ascii"))
b'x\x9c\xf3H\xcd\xc9\xc9\xd7Q(\xcf/\xcaI\x01\x00\x1b\xd4\x04i'
The same string could describe different byte sequences otherwise. But it is actually a byte sequence that will be encoded by zlib.
>>> zlib.compress("Hello, wørld".encode("utf-16"))
b'x\x9c\xfb\xff\xcf\x83!\x95!\x07\x08\xf3\x19t\x18\x14\x18\xca\x19~0\x14\x01y)\x0c\x00n\xa6\x06\xef'
>>> zlib.compress("Hello, wørld".encode("utf-8"))
b"x\x9c\xf3H\xcd\xc9\xc9\xd7Q(?\xbc\xa3('\x05\x00#\x7f\x05u"
In python 2.x strings are bytes string by default. In python 3.x they are unicode strings.
Compressing needs a byte string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With