日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 2190 0

本文實例講述了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è)計有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 岚皋县| 龙井市| 灌南县| 武义县| 汕尾市| 涞水县| 尚义县| 桑日县| 灌阳县| 汕头市| 青岛市| 集安市| 保康县| 岢岚县| 揭东县| 宁化县| 通州区| 平南县| 特克斯县| 永宁县| 静海县| 定兴县| 辉南县| 沈阳市| 涟水县| 桦南县| 郑州市| 屏南县| 许昌县| 澄迈县| 正安县| 周至县| 道孚县| 仁怀市| 潮安县| 万全县| 东乡县| 农安县| 平遥县| 湖北省| 巨鹿县|