views:

95

answers:

2

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

+2  A: 

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"
relet
Curious: why does this work for me (Python 3.0.1, Ubuntu Jaunty) without calling `encode`?
Manoj Govindan
Check the release docs, this change might be enforced in a later version of 3.
Dana the Sane
A: 

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

Compressing needs a byte string.

Douglas Leeder