在Python中无法使用Snappy解压缩
在我的项目中,我使用了Snappy这个工具来压缩Python中的HTML页面。我已经成功地压缩了这些HTML页面。html_page
是包含网站HTML字符串的变量。
import json
import snappy
state_dict["html_page"] = unicode(snappy.compress(html_page),errors="ignore")
"""
If i miss this unicode function
UnicodeDecodeError: 'utf8' codec can't decode byte 0xbc in position 0: unexpected code byte
"""
........
........
return json.dumps(state_dict)
但是我在解压缩这些压缩数据时遇到了问题:
d = json.loads(mydict)
snappy.uncompress(d['html_page'].encode("utf-8"))
In [122]: snappy.uncompress(d['html_page'].encode("utf-8"))
---------------------------------------------------------------------------
UncompressError Traceback (most recent call last)
/home/gridlex/workspace/MatrixInfrastructure/<ipython console> in <module>()
UncompressError: An error ocurred while uncompressing the string
你能帮我解决Snappy的压缩和解压缩问题吗?或者有没有更好的方法可以用来在网络上传输数据时进行压缩和解压缩?
1 个回答
0
试试这个:
>>> state_dict['test'] = snappy.compress('random string with ö, random string with ö, random string with ö, random string with ö, random string with ö, random string with ö, random string with ö, random string with ö, random string with ö, ')
>>> dump = json.dumps(state_dict, encoding='ISO-8859-2')
>>> load = json.loads(dump, encoding='ISO-8859-2')
>>> snappy.uncompress(load['test'].encode('ISO-8859-2'))