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 ...
随机推荐
- jmeter beanshell 写入文件
1.首先F:\test.txt文件为空
- Jmeter接口测试加解密及Bean Shell使用案例
Jmeter接口测试加解密及Bean Shell使用案例 https://blog.csdn.net/russ44/article/details/56009084 本文以base64加解密为例: 一 ...
- 控制 if 语句 while循环 break continue
if 语句的语法: 1. if 条件 : #引号是将条件与结果分开 代码块 # 四个空格,或者一个tab键,这个是告诉程序满足这个条件的 说明: 当条件成立的时候(True), 代码块会被执行 ...
- 字典dict详解
字典也是 Python 提供的一种常用的数据结构,它用于存放具有映射关系的数据. 比如有份成绩表数据,语文:79,数学:80,英语:92,这组数据看上去像两个列表,但这两个列表的元素之间有一定的关联关 ...
- unity3D中的Input按键方法检测
一,按键的按下抬起等识别方法 void Update () { ; ; if (Input.GetKeyDown (KeyCode.A)) { Debug.Log("A按下一次") ...
- 回车\r的含义
package main import "fmt" func main() { // \r 回车,从当前行的最前面开始输出,覆盖掉以前的内容 // 输出:曹操刘备关羽 fmt.Pr ...
- 查看linux 用户登录信息及ip
查看可疑IP登陆 last -f /var/log/wtmp cat /var/log/secure 寻找可疑ip登陆次数及信息 who 查看当前登陆用户 -h 忽略头文件信息 -u 显示结 ...
- VS2013+phread.h环境配置
原文链接:http://blog.csdn.net/qianchenglenger/article/details/16907821 本人使用的是windows7 旗舰版64位 目前用的是pthrea ...
- 【leetcode】926.Flip String to Monotone Increasing
题目如下: A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possib ...
- beautifhulsoup4的使用
Beautiful: - 基本使用 from bs4 import BeautifulSoup - 解析器: lxml, html.parser soup = Beautiful ...