python bz2压缩字符串

时间:2015-12-23 05:23:15

标签: python python-3.x bz2

>>> import bz2
>>> bz2.compress('hi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ryan/anaconda/lib/python3.4/bz2.py", line 498, in compress
return comp.compress(data) + comp.flush()
TypeError: 'str' does not support the buffer interface

我见过使用字符串作为输入的例子,但它对我不起作用

1 个答案:

答案 0 :(得分:6)

压缩算法压缩字节not text

3>> bz2.compress(b'hi')
b'BZh91AY&SY\x9a\x89\xb4"\x00\x00\x00\x01\x00\x00` \x00!\x00\x82\xb1w$S\x85\t\t\xa8\x9bB '
3>> bz2.compress('hi'.encode('utf-8'))
b'BZh91AY&SY\x9a\x89\xb4"\x00\x00\x00\x01\x00\x00` \x00!\x00\x82\xb1w$S\x85\t\t\xa8\x9bB '