使用 python 解压缩 gzip 压缩字典对象

Decompress a gzip compressed dictionary object using python

我需要解压这个“H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr+R0AAAA=”,它实际上是{“test1”:12, “test2”: “test”}的压缩形式。现在 python 我正在使用 gzip 库并得到下面提到的响应:

>>> import gzip
>>> gzip.decompress("H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr+R0AAAA=".encode("UTF-8"))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 548, in decompress
    return f.read()
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 292, in read
    return self._buffer.read(size)
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 479, in read
    if not self._read_gzip_header():
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 427, in _read_gzip_header
    raise BadGzipFile('Not a gzipped file (%r)' % magic)
gzip.BadGzipFile: Not a gzipped file (b'H4')

有什么方法可以解压python中的字符串吗?

该字符串是 Base64 编码的。因此:-

import gzip
import base64

b = base64.b64decode('H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr+R0AAAA=')
r = gzip.decompress(b)
print(r.decode())