python中用zlib解压报错zlib.error: Error -3 while decompressing: incorrect header check的原因和解决方法

本文探讨了在Python中,使用str()方法将base64编码的字节串转换时可能出现的zlib解压错误。关键在于正确指定编码方式,以避免'incorrectheadercheck'错误。通过实例和StackOverflow链接,提供了修复方法。
部署运行你感兴趣的模型镜像

形如下面的一段代码:

b64_encoded_bytes = base64.b64encode(zlib.compress(b'abcde'))
encoded_bytes_representation = str(b64_encoded_bytes)  # this the cause
zlib.decompress(base64.b64decode(encoded_bytes_representation))

zlib.decopress时报错

zlib.error: Error -3 while decompressing: incorrect header check

原因在于str()方法的使用
python3文档中对str()方法的描述:
If neither encoding nor errors is given, str(object) returns object.str(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a str() method, then str() falls back to returning repr(object).

str()方法的作用只是把

b'eJxLTEpOSQUABcgB8A=='

转换成了

"b'eJxLTEpOSQUABcgB8A=='"

而正确的转换应该是转换为

"eJxLTEpOSQUABcgB8A=="

解决方法是:
str()方法中指定编码方式

str(b64_encoded_bytes, 'utf-8')

可以参考https://stackoverflow.com/questions/3122145/zlib-error-error-3-while-decompressing-incorrect-header-check

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值