bal*_*ath 3 compression zlib python-3.x
我正在尝试使用 zlib 在 python 3.6.3 中压缩一个字符串,但是得到一个错误(TypeError: a bytes-like object is required, not 'str') ,它应该在 python 2.7- 版本上工作,这是我的简单代码:
import zlib
a='hellohellohelloheeloohegregrf'
b=zlib.compress(a)
print(b)
Run Code Online (Sandbox Code Playgroud)
import zlib
a='hellohellohelloheeloohegregrf'
b=zlib.compress(a.encode("utf-8"))
print(b)
Run Code Online (Sandbox Code Playgroud)
选择:
import zlib
a= b'hellohellohelloheeloohegregrf'
b=zlib.compress(a)
print(b)
Run Code Online (Sandbox Code Playgroud)
在Python2.x这个字符串中,文字被称为str对象,但它存储为bytes.
在Python3.x这个字符串中,文字是一个str对象,它的类型是Unicode. 因此,需要在它前面加上前缀b或用于.encode获取bytes对象。
| 归档时间: |
|
| 查看次数: |
1328 次 |
| 最近记录: |