Python3使用zlib对网页文本进行压缩解压

1,278 阅读1分钟

业务场景:选择存储网页是应对产品频繁变更需求,使用zlib压缩网页文本是为了节约磁盘空间。

压缩

import zlib

text = '网页文本'
compressed = zlib.compress(text.encode('utf-8'))
print(compressed)

解压

decompressed = zlib.decompress(compressed)
print(decompressed.decode('utf-8'))

如果出现异常SyntaxError: Non-UTF-8 code starting with '\xe4',请在py文件第一行添加

# -*- coding: utf-8 -*-

其它压缩模块zlib,zip,zipfile,gzip