国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python用模塊zlib壓縮與解壓字符串和文件的方法

2019-11-25 16:27:03
字體:
來源:轉載
供稿:網友

python中zlib模塊是用來壓縮或者解壓縮數據,以便保存和傳輸。它是其他壓縮工具的基礎。下面來一起看看python用模塊zlib壓縮與解壓字符串和文件的方法。話不多說,直接來看示例代碼。

例子1:壓縮與解壓字符串

import zlibmessage = 'abcd1234'compressed = zlib.compress(message)decompressed = zlib.decompress(compressed)print 'original:', repr(message)print 'compressed:', repr(compressed)print 'decompressed:', repr(decompressed)

結果

original: 'abcd1234'compressed: 'x/x9cKLJN1426/x01/x00/x0b/xf8/x02U'decompressed: 'abcd1234'

例子2:壓縮與解壓文件

import zlibdef 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())if __name__ == "__main__": compress('in.txt', 'out.txt') decompress('out.txt', 'out_decompress.txt')

結果

生成文件

out_decompress.txt out.txt

問題――處理對象過大異常

>>> import zlib>>> a = '123'>>> b = zlib.compress(a)>>> b'x/x9c342/x06/x00/x01-/x00/x97'>>> a = 'a' * 1024 * 1024 * 1024 * 10>>> b = zlib.compress(a)Traceback (most recent call last): File "<stdin>", line 1, in <module>OverflowError: size does not fit in an int

總結

以上就是關于python模塊zlib壓縮與解壓的全部內容,希望本文的內容對大家學習或者使用python能有一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永寿县| 甘南县| 新干县| 澄迈县| 鹰潭市| 连山| 南雄市| 来凤县| 马鞍山市| 祁东县| 静宁县| 阜宁县| 凤山市| 桦甸市| 巴林右旗| 新河县| 江川县| 丽水市| 台东县| 衡阳市| 南涧| 泰州市| 驻马店市| 任丘市| 江源县| 张家界市| 衢州市| 和静县| 乐安县| 盐城市| 雷波县| 辽阳县| 安远县| 富平县| 白银市| 定远县| 宾川县| 平利县| 三河市| 冀州市| 濉溪县|