Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compress a string in python 3?

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

like image 524
Bussiere Avatar asked Oct 01 '10 12:10

Bussiere


People also ask

How do you compress a string?

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.


2 Answers

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"
like image 124
relet Avatar answered Oct 14 '22 13:10

relet


In python 2.x strings are bytes string by default. In python 3.x they are unicode strings.

Compressing needs a byte string.

like image 38
Douglas Leeder Avatar answered Oct 14 '22 13:10

Douglas Leeder