我正在尝试使用zlib在Python 3.6.3中压缩字符串,但是出现了错误(TypeError:需要类似字节的对象,而不是'str'),它应该在Python 2.7及以下版本上运行,这是我的简单代码:
import zlib
a='hellohellohelloheeloohegregrf'
b=zlib.compress(a)
print(b)
我正在尝试使用zlib在Python 3.6.3中压缩字符串,但是出现了错误(TypeError:需要类似字节的对象,而不是'str'),它应该在Python 2.7及以下版本上运行,这是我的简单代码:
import zlib
a='hellohellohelloheeloohegregrf'
b=zlib.compress(a)
print(b)
import zlib
a='hellohellohelloheeloohegregrf'
b=zlib.compress(a.encode("utf-8"))
print(b)
替代方案:
import zlib
a= b'hellohellohelloheeloohegregrf'
b=zlib.compress(a)
print(b)