麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

python通過zlib實現壓縮與解壓字符串的方法

2020-02-23 06:12:24
字體:
來源:轉載
供稿:網友

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

使用zlib.compress可以壓縮字符串。使用zlib.decompress可以解壓字符串。如下
代碼如下:#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:
代碼如下: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對文件進行壓縮解壓
代碼如下: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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91精品国产乱码久 | 新久草在线视频 | 国产精品美女久久久久久网站 | 国产亚洲精品久久久久久网站 | 精品999久久久 | 亚洲尻逼视频 | 国产一区视频在线免费观看 | h色视频在线观看 | 久久久久久久免费视频 | av电影在线观看网站 | 久久爽精品区穿丝袜 | 免费观看亚洲视频 | 久久草在线视频国产 | 性生活视频一级 | 免费男女乱淫真视频 | 鲁人人人鲁人人鲁精品 | 毛片毛片 | 国产亚洲激情 | 国产亚洲精彩视频 | 羞羞视频一区 | 久久久久久久久成人 | 欧产日产国产精品v | 久色婷婷| 欧美爱爱一区二区 | 72pao成人国产永久免费视频 | 男人天堂免费 | 欧美日本免费一区二区三区 | 国产最新网站 | 日本在线观看中文字幕 | 久久草在线视频免费 | 成人国产在线看 | 毛片在线看免费 | 在线观看第一区 | 九一成人 | 特黄一区二区三区 | 久草成人在线观看 | 亚洲欧美国产精品va在线观看 | 国产日韩精品欧美一区视频 | 毛片观看网址 | 精品国产精品久久 | 欧美zoofilia杂交videos |