Python - 压缩Ascii字符串

时间:2012-10-13 09:26:08

标签: python algorithm compression

我正在寻找一种压缩基于ascii的字符串的方法,有什么帮助吗?

我还需要解压缩它。我试过zlib但没有帮助。

如何将字符串压缩为较短的长度?

代码:

def compress(request):
    if request.POST:
        data = request.POST.get('input')
        if is_ascii(data):
            result = zlib.compress(data)
            return render_to_response('index.html', {'result': result, 'input':data}, context_instance = RequestContext(request))
        else:
            result = "Error, the string is not ascii-based"
            return render_to_response('index.html', {'result':result}, context_instance = RequestContext(request))
    else:
        return render_to_response('index.html', {}, context_instance = RequestContext(request))

2 个答案:

答案 0 :(得分:25)

使用压缩并不总是会减少字符串的长度!

考虑以下代码;

import zlib
import bz2

def comptest(s):
    print 'original length:', len(s)
    print 'zlib compressed length:', len(zlib.compress(s))
    print 'bz2 compressed length:', len(bz2.compress(s))

让我们试一下空字符串;

In [15]: comptest('')
original length: 0
zlib compressed length: 8
bz2 compressed length: 14

因此zlib产生额外的8个字符,bz2 14.压缩方法通常在压缩数据前放置一个“标题”供解压缩程序使用。此标头会增加输出的长度。

让我们测试一个单词;

In [16]: comptest('test')
original length: 4
zlib compressed length: 12
bz2 compressed length: 40

即使您减去标题的长度,压缩也没有使单词变短。那是因为在这种情况下压缩很少。字符串中的大多数字符只出现一次。现在是一个短句;

In [17]: comptest('This is a compression test of a short sentence.')
original length: 47
zlib compressed length: 52
bz2 compressed length: 73

压缩输出再次大于输入文本。由于文本的长度有限,因此几乎没有重复,所以它不能很好地压缩。

你需要一个相当长的文本块来进行压缩才能真正起作用;

In [22]: rings = '''
   ....:     Three Rings for the Elven-kings under the sky, 
   ....:     Seven for the Dwarf-lords in their halls of stone, 
   ....:     Nine for Mortal Men doomed to die, 
   ....:     One for the Dark Lord on his dark throne 
   ....:     In the Land of Mordor where the Shadows lie. 
   ....:     One Ring to rule them all, One Ring to find them, 
   ....:     One Ring to bring them all and in the darkness bind them 
   ....:     In the Land of Mordor where the Shadows lie.'''

In [23]: comptest(rings)                       
original length: 410
zlib compressed length: 205
bz2 compressed length: 248

答案 1 :(得分:6)

您甚至不需要数据为ascii,您可以使用任何内容提供zlib

>>> import zlib
>>> a='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' # + any binary data you want
>>> print zlib.compress(a)
x�KL$
�
>>>

你可能想要的是什么 - 压缩数据是ascii字符串?我在这儿吗? 如果是这样 - 你应该知道你有一个非常小的字母表来编码压缩数据=>所以你会使用更多的符号。

例如,在base64中编码二进制数据(你将获得ascii字符串),但你将使用大约30%的空间