Python - 压缩 Ascii 字符串

标签 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))

最佳答案

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

考虑以下代码;

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. 压缩方法通常在压缩数据前放置一个 'header' 以供解压缩程序使用。此 header 增加了输出的长度。

让我们测试一个单词;

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

再次压缩输出比输入文本。由于文字篇幅有限,里面的重复很少,所以不会很好压缩。

您需要相当长的文本 block 才能真正进行压缩;

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 - 压缩 Ascii 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871775/

相关文章:

python - Flask url 中的正则表达式

algorithm - n维空间爬山: finding the neighbor

php数组预设值插入

iphone - 启用 UIImagePickerController 视频压缩

python - Tensorflow 上的 GPU 是否进行了非极大值抑制?

python - 在python中快速读取HDF 5文件?

algorithm - 查找不包括某些对的连续子数组的数量

unix - tar 和 zip 有什么区别?

android - 是否有可能以编程方式在 android 中压缩音频和视频文件?

python - 从python中的列表元素中删除字符