python使用zlib实现压缩与解压字符串
命令
字符串:使用zlib.compress可以压缩字符串。使用zlib.decompress可以解压字符串。
数据流:压缩:compressobj,解压:decompressobj
案例
>>> import zlib
>>> s = 'slfsjdalfkasflkkdkaleeeeeeeeeeeeeeeeeeeeeeeeeeeelaaalkllfksaklfasdll kkkkkk123'
>>> zlib_s = zlib.compress(s)
>>> zlib_s
'x\x9c}\xca\xb1\r\xc0 \x10\x04\xc1Vh\xc1\xb8\xa2\x93\x9e\x0f|\x9b]\xff\x92\x11\x050\xf1\x84\xceW\xa2\xad4vY\xac\x0b$a\xf6\x8fL+\x05c\xf8x\xe6\xfb\x03\xf7\x97\x1e\xd1' >>> print tlen(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tlen' is not defined
>>> print len(s)
79
>>> print len(zlib_s)
55
>>> ss = zlib.decompress(zlib_s)
>>> ss
'slfsjdalfkasflkkdkaleeeeeeeeeeeeeeeeeeeeeeeeeeeelaaalkllfksaklfasdll kkkkkk123'
压缩与解压缩文件
import zlib
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()) if __name__ == "__main__":
infile = "1.txt"
dst = "1.zlib.txt"
compress(infile, dst) infile = "1.zlib.txt"
dst = "2.txt"
decompress(infile, dst)
print "done~"
注:compressobj返回一个压缩对象,用来压缩不能一下子读入内存的数据流。 level 从9到-1表示压缩等级,其中1最快但压缩度最小,9最慢但压缩度最大,0不压缩,默认是-1大约相当于与等级6,是一个压缩速度和压缩度适中的level。
python使用zlib实现压缩与解压字符串的更多相关文章
- python用模块zlib压缩与解压字符串和文件的方法
摘自:http://www.jb51.net/article/100218.htm Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等. python中 ...
- python tar.gz格式压缩、解压
一.压缩 需求描述 现在有一个目录,需要将此目录打包成tar.gz文件.因为有一个Django项目,需要用到此功能! tar.gz 目录结构如下: ./ ├── folder │ ├── .doc ...
- C# 压缩与解压字符串(面试题)
/* * 题目:压缩字符串.如“abbcccddddeef”,压缩成“a1b2c3d4e2f1” * 解题: 这个题目也是面试常见的题目.看似很简单,其实暗藏杀机.一般的想法就是,一边遍历,一边计数, ...
- zlib的压缩与解压
http://zlibnet.codeplex.com/releases/view/629717 using ZLibNet; string str = "ccc"; byte [ ...
- python 模块zlib 压缩与解压
例子1:压缩与解压字符串 import zlib message = 'abcd1234' compressed = zlib.compress(message) decompressed = zli ...
- PAT 乙级 1078 字符串压缩与解压 (20)
文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 ...
- PAT 1078 字符串压缩与解压(20)(代码+思路)
1078 字符串压缩与解压(20 分) 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表 ...
- PAT 1078 字符串压缩与解压
https://pintia.cn/problem-sets/994805260223102976/problems/994805262018265088 文本压缩有很多种方法,这里我们只考虑最简单的 ...
- PAT(B) 1078 字符串压缩与解压(Java)
题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...
随机推荐
- ImageMagick jmagick 安装
在安装ImageMagick之前,请检查下面包已经安装 tiff-3.9.5.tar.gz (rpm -qa|grep libtiff检查是否已经安装) libpng-1.2.46.t ...
- mysql的事务处理与锁表
数据库的事务处理可以保证一组处理结果的正确性.mysql中只有INNODB和BDB引擎的数据表才支持事务处理,对于不支持事务的MyISAM引擎数据库可以使用表锁定的方法来实现相同的功能. mysql的 ...
- PHP 错误与异常 笔记与总结(5)配置文件中与错误日志相关的选项 && 将错误记录到指定的文件中
[记录错误(生产环境)] php.ini: ① 开启 / 关闭 错误日志功能 log_errors = On ② 设置 log_errors 的最大字节数 log_errors_max_len = 其 ...
- 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。
笔记本装的是windows 7旗舰版64位系统,使用的是MS Office 2007(Microsoft.ACE.OLEDB.12.0,32位程序),开发用的是Visual Studio 2010,我 ...
- JS Questions:Front-end Developer Interview Questions
Explain event delegation Event delegation allows us to attach a single event listener, to a parent e ...
- P2672 推销员
贪心,水题 #include <bits/stdc++.h> using namespace std; const int maxn = 1000000; struct house { i ...
- JAVA函数的返回值类型详解以及生成随机数的例题
函数的四要素:函数名.输入.输出(返回).加工. 函数分为两种:一种是有返回值得函数,一种是没有返回值的函数. 1. 定义:没有返回值的函数:(当我不需要函数的计算结果再拿出来进行运算的时候,我就不需 ...
- activitydialog
给Activity设置Dialog属性,点击区域外消失:,activitydialog 1.在AndroidManifest.xml中给Activity设置样式: <activity ...
- SQLite(快速上手版)笔记
原文 1. SQL语法关键字 关键字 描述 Create Table 创建数据表 Alter Table 修改数据表 Drop Table 删除数据表 Create Index 创建索引 Drop I ...
- 深入GetMessage和PeekMessage
http://blog.csdn.net/fireseed/article/details/2176 http://www.cnblogs.com/sadier/articles/100948.htm ...