gpt4 book ai didi

python-3.x - python 3.6.3。 zlib压缩

转载 作者:行者123 更新时间:2023-12-04 02:55:04 26 4
gpt4 key购买 nike

我正在尝试使用 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 对象。

关于python-3.x - python 3.6.3。 zlib压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com