import os
import gzip
import io, json
from io import BytesIO
压缩
cwd = os.getcwd()
fn = str(“test”) + ‘.gz’
fn = os.path.join(cwd, fn)
item = {“q”: “jfhjakfhj”, “j”: “fkFKLFOPMPLMSM”}
with gzip.open(fn, ‘wb’) as g:
with io.TextIOWrapper(g, encoding=‘utf-8’) as ec:
ec.write(json.dumps(item))
解压1
print(cwd)
fn = cwd + “\test.gz”
with gzip.open(fn, ‘rb’) as g:
with io.TextIOWrapper(g, encoding=‘utf-8’) as ec:
item = ec.read()
print(json.loads(item))
解压2
fn = cwd + “\test.gz”
with open(fn, “rb”) as f:
r = f.read()
print®
buff = BytesIO®
f = gzip.GzipFile(fileobj=buff)
big_result = json.loads(f.read().decode(‘utf-8’))
print(big_result)
request获取压缩文件流
res = request.post(data=data,headers=headers)
res = res.content
buff = BytesIO®
f = gzip.GzipFile(fileobj=buff)
big_result = json.loads(f.read().decode(‘utf-8’))