【问题标题】:Python - Compress Ascii StringPython - 压缩 Ascii 字符串
【发布时间】:2012-10-13 09:26:07
【问题描述】:

我正在寻找一种压缩基于 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))

【问题讨论】:

标签: python algorithm compression


【解决方案1】:

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

考虑以下代码;

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

【讨论】:

  • 请注意,对于 Python 3,zlib.compressbz2.compress 的输入必须以字节为单位,因此您必须首先输入 .encode() 字符串
【解决方案2】:

你甚至不需要你的数据是 ascii,你可以给 zlib 提供任何东西

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

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

例如,以 base64 编码二进制数据(您将获得 ascii 字符串),但您将为此多使用约 30% 的空间

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多