形如下面的一段代码:
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')
本文探讨了在Python中,使用str()方法将base64编码的字节串转换时可能出现的zlib解压错误。关键在于正确指定编码方式,以避免'incorrectheadercheck'错误。通过实例和StackOverflow链接,提供了修复方法。
2026

被折叠的 条评论
为什么被折叠?



