You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中解压Gzipped格式的字符串?

Python解压Base64编码的Gzip字符串方法

你遇到的问题是因为给出的字符串是Base64编码后的Gzip压缩数据,gzip.decompress()需要的是原始字节格式的压缩数据,而非Base64字符串,直接调用会报错。

正确的处理步骤是先对字符串做Base64解码,得到字节类型的压缩数据,再用gzip.decompress()解压,最后转换为普通字符串。

完整代码示例:

import gzip
import base64

string = 'H4sIAAAAAAAAClWPwcrCQAyEX2WOCtJ3+EXw5E3wvLZjG4gb2WR/tU/vFlEQQiATvszkRO3tSoThUDWkhmiH4ySOVjEtG1PcJxbiaRV9ymjErdB9EQqCj4AVDPzVPxMH/CetRHXJI/az3FbjvN5gx0vS4MJui4XKFxHLSDpakZiu3uFP3X7cU0uzhHOZiTPjTua3q7fWEykPaJfaS5CM8zPo3QvUlCS87AAAAA=='
compressed_bytes = base64.b64decode(string)
result = gzip.decompress(compressed_bytes).decode()
print(result)

运行后会输出你预期的文本:

Welcome to Multiutil. This is the tool where you can compress your text or decompress your compressed value using Gzip(gz), Defalte or Brotli compression algorithms. Also you can compare the size between your source and result in bytes.

内容的提问来源于stack exchange,提问作者jakeMantle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:30:40