最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python通過zlib實現(xiàn)壓縮與解壓字符串的方法

 更新時間:2014年11月19日 11:15:52   投稿:shichen2014  
這篇文章主要介紹了python通過zlib實現(xiàn)壓縮與解壓字符串的方法,較為詳細的介紹了zlib的用法及使用zlib.compressobj和zlib.decompressobj對文件進行壓縮解壓的方法,需要的朋友可以參考下

本文實例講述了python通過zlib實現(xiàn)壓縮與解壓字符串的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

使用zlib.compress可以壓縮字符串。使用zlib.decompress可以解壓字符串。如下

復(fù)制代碼 代碼如下:
#coding=utf-8
import zlib
s = "hello word, 00000000000000000000000000000000"
print len(s)
c = zlib.compress(s)
print len(c)
d =  zlib.decompress(c)
print d

 
示范代碼2:
復(fù)制代碼 代碼如下:
import zlib
message = 'witch which has which witches wrist watch'
compressed = zlib.compress(message)
decompressed = zlib.decompress(compressed)
print 'original:', repr(message)
print 'compressed:', repr(compressed)
print 'decompressed:', repr(decompressed) #輸出original: 'witch which has which witches wrist watch'
compressed: 'xx9c+xcf,IxceP(xcfxc8x04x92x19x89xc5PV9H4x15xc8+xca,.Q(Ox04xf2x00D?x0fx89'
decompressed: 'witch which has which witches wrist watch'

如果我們要對字符串進行解壓可以使用zlib.compressobj和zlib.decompressobj對文件進行壓縮解壓
復(fù)制代碼 代碼如下:
def compress(infile, dst, level=9):
    infile = open(infile, 'rb')
    dst = open(dst, 'wb')
    compress = zlib.compressobj(level)
    data = infile.read(1024)
    while data:
        dst.write(compress.compress(data))
        data = infile.read(1024)
    dst.write(compress.flush())
def decompress(infile, dst):
    infile = open(infile, 'rb')
    dst = open(dst, 'wb')
    decompress = zlib.decompressobj()
    data = infile.read(1024)
    while data:
        dst.write(decompress.decompress(data))
        data = infile.read(1024)
    dst.write(decompress.flush())

希望本文所述對大家的Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

兰溪市| 怀宁县| 张家川| 贡山| 怀集县| 依兰县| 潢川县| 礼泉县| 沽源县| 梅河口市| 永靖县| 依兰县| 呼伦贝尔市| 太原市| 漳州市| 湖北省| 永泰县| 滕州市| 商城县| 无为县| 仙居县| 正定县| 永年县| 锦屏县| 泾川县| 三原县| 茶陵县| 宝清县| 贡觉县| 安化县| 新巴尔虎左旗| 青阳县| 东丰县| 亚东县| 巴南区| 石城县| 宁城县| 盐边县| 金湖县| 金秀| 荣成市|