python 3.6.3。 zlib压缩
python 3.6.3. zlib compression
我正在尝试使用 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)
在 Python2.x
中,此字符串文字称为 str
对象,但它存储为 bytes
.
在 Python3.x
中,这个字符串文字是一个 str
对象,它的类型是 Unicode
。因此,需要在其前面加上 b
或使用 .encode
来获取 bytes
对象。
我正在尝试使用 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)
在 Python2.x
中,此字符串文字称为 str
对象,但它存储为 bytes
.
在 Python3.x
中,这个字符串文字是一个 str
对象,它的类型是 Unicode
。因此,需要在其前面加上 b
或使用 .encode
来获取 bytes
对象。