无法在Python中通过Snappy解压缩

时间:2013-01-25 08:34:40

标签: python unicode snappy

在我的项目中,我在Python的HTML页面中使用Snappy压缩。我正在成功压缩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 :(得分: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'))
相关问题