简体   繁体   中英

python bz2 compression of string

>>> 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

I've seen examples using strings as input but it wont work for me

Compression algorithms compress bytes, 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 '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM