python 数据压缩
zlib 压缩
import zlib
import this
s = this.s.encode('utf8')*10
for i in range(10):
data = zlib.compress(s,i) #compress 接收两个参数分别是要压缩的字节和压缩等级。
de_data = zlib.decompress(data) #解压缩
print(f"data:{len(data)},s:{len(s)}")
结果如下:
data:8571,s:8560
data:562,s:8560
data:560,s:8560
data:558,s:8560
data:519,s:8560
data:511,s:8560 #可以看出压缩到极限以后无法在继续压缩
data:511,s:8560
data:511,s:8560
data:511,s:8560
data:511,s:8560
这个压缩方法有一个明显的缺陷:需要有足够大的内存去存储待压缩数据和压缩后的数据。那我们是否可以每次压缩一部分呢,也是可以的
import zlib
import this
s = this.s*10
with open('a.txt','w') as t:
t.write(s)
com = zlib.compressobj()
with open('a.txt', 'rb') as f:
while True:
a = f.read(64)
if not a:
break
data = com.compress(a)
if data:
print(f"data:{len(data)}")
else:
print("doing....")
result = com.flush()
print(f"result:{len(result)}")
结果如下:
doing....
doing....
doing....
doing....
doing....
doing....
doing....
doing....
result:515
gzip 压缩数据
gzip 和 zlib都有compress和deconpress方法,用法也是一样的,说说文件的操作把
读取压缩文件示例
import gzip
with gzip.open('file.txt.gz', 'rb') as f:
file_content = f.read()
创建压缩GZIP文件的示例:
import gzip
content = "Lots of content here"
with gzip.open('file.txt.gz', 'wb') as f:
f.write(content)
GZIP压缩现有文件的示例:
import gzip
import shutil
with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
bz2压缩
bz2.compress
bz2.decompress
基本与zlib一样不多说
tarfile 压缩数据
如何将整个tar存档解压缩到当前工作目录:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
如何TarFile.extractall()使用生成器函数而不是列表来提取tar存档的子集:
import os
import tarfile def py_files(members):
for tarinfo in members:
if os.path.splitext(tarinfo.name)[1] == ".py":
yield tarinfo tar = tarfile.open("sample.tar.gz")
tar.extractall(members=py_files(tar))
tar.close()
如何从文件名列表创建未压缩的tar存档:
import tarfile
tar = tarfile.open("sample.tar", "w")
for name in ["foo", "bar", "quux"]:
tar.add(name)
tar.close()
使用with语句的相同示例:
import tarfile
with tarfile.open("sample.tar", "w") as tar:
for name in ["foo", "bar", "quux"]:
tar.add(name)
如何阅读gzip压缩的tar存档并显示一些成员信息:
import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
tar.close()
如何使用以下过滤器 参数创建存档并重置用户信息TarFile.add():
import tarfile
def reset(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
tar = tarfile.open("sample.tar.gz", "w:gz")
tar.add("foo", filter=reset)
tar.close()
python 数据压缩的更多相关文章
- 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸
类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...
- 如何使用Python在Kaggle竞赛中成为Top15
如何使用Python在Kaggle竞赛中成为Top15 Kaggle比赛是一个学习数据科学和投资时间的非常的方式,我自己通过Kaggle学习到了很多数据科学的概念和思想,在我学习编程之后的几个月就开始 ...
- python学习笔记系列----(八)python常用的标准库
终于学到了python手册的最后一部分:常用标准库.这部分内容主要就是介绍了一些基础的常用的基础库,可以大概了解下,在以后真正使用的时候也能想起来再拿出来用. 8.1 操作系统接口模块:OS OS模块 ...
- 《Python标准库》 目录
目录 译者序序前言第1章 文本1.1 string—文本常量和模板1.1.1 函数1.1.2 模板1.1.3 高级模板1.2 textwrap—格式化文本段落1.2.1 示例数据1.2.2 填充段落1 ...
- 【数据压缩】LZ77算法原理及实现
1. 引言 LZ77算法是采用字典做数据压缩的算法,由以色列的两位大神Jacob Ziv与Abraham Lempel在1977年发表的论文<A Universal Algorithm for ...
- 【数据压缩】Huffman编码
1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...
- Python 入门指南
Release: 3.4 Date: March 29, 2014 Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构,并且能够用简单而又高效的方式进行面向对象编程. Pyt ...
- python实现简易数据库之一——存储和索引建立
最近没事做了一个数据库project,要求实现一个简单的数据库,能满足几个特定的查询,这里主要介绍一下我们的实现过程,代码放在过ithub,可参看这里.都说python的运行速度很慢,但因为时间比较急 ...
- python标准库介绍
操作系统接口 os模块提供了不少与操作系统相关联的函数. >>> import os >>> os.getcwd() # 返回当前的工作目录 'C:\\Python ...
随机推荐
- SSL连接出现的问题
客户端向服务器发送数据时,份两种情况,SSL单向验证和SSL双向验证 1.SSL单向验证时 代码如下: import java.io.IOException; import java.util.Has ...
- 解决“每次打开office2010的word都会出现配置进度框”问题
在win7中安装完office2010后.打开 *.doc文件时,总会弹出"配置进度框"问题,解决例如以下: 1)点击"開始"-->"执 ...
- java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation getting while running test project?
转摘:http://stackoverflow.com/questions/11155340/java-lang-illegalaccesserror-class-ref-in-pre-verifie ...
- 饿汉单例模式 and 懒汉单例模式
饿汉单例模式:主要就是利用static关键字,在类加载的时候生成实例,调用效率高 但是如果一直没有调用getInstance方法的话,就会造成资源浪费 具体实现如下: class Single{ pr ...
- Linux xargs 命令
xargs xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具. xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据. xargs 也可 ...
- 离线安装gcc_rpm(centos下安装gcc的方法之一)
.解压gcc_rpm.tar.gz (我的CSDN账号下载过) tar -zxvf gcc_rpm.tar.gz .解压完进入文件夹,执行以下命令,挨个执行(如果报依赖错误,就加上"--fo ...
- ArrayList详解,底层是数组,实现Serializable接口
一.对于ArrayList需要掌握的七点内容 ArrayList的创建:即构造器往ArrayList中添加对象:即add(E)方法获取ArrayList中的单个对象:即get(int index)方法 ...
- Update Vim to 8.0 in Ubuntu
add PPA sudo add-apt-repository ppa:jonathonf/vim Update and Install sudo apt-get update sudo apt-ge ...
- 【持久层】Druid简介
Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss DataSou ...
- 【Flutter学习】页面跳转之SliverAppBar,CustomScrollView,NestedScrollView的使用
一,flutter SliverAppbar 控件介绍 SliverAppBar “应用栏” 相当于升级版的 appbar 于 AppBar 位置的固定的应用最上面的; 而 SliverAppBar ...