1

在我的项目中,我在 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 的压缩和解压问题吗?或者哪种是通过网络传输数据的最佳压缩和解压缩方式?

4

1 回答 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'))
于 2013-01-25T16:31:09.823 回答