简体   繁体   中英

How to decode gzip STRING?

I've handling the SMTP server input emails log with my unittest and have the gzip data in email attachment. As i need to exclude unnecessary data from log i open the file with r mode. In the result i have the next STRING for attached data to email

b'H4sIAPjl018C/wvJyCxWAKKUxJJEhbT8IoWCxKKSzJLM/DwFQwB9NabZHAAAAA=='

the type -- string!

Do the exist way to decode in python this STRING from gzip to simple string representation? the decode should be

This is data for partition 1

It looks like the data you're working with is actually base64-encoded after being compressed with gzip, so it's necessary to reverse both the encoding and the compression to get back the original content. You can use the base64 and gzip libraries, like so:

import base64
import gzip

encoded = ... # Get the encoded data somehow
compressed = base64.b64decode(encoded)
original = gzip.decompress(compressed)
print(original) # b'This is the data for partition 1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM