Python Gzip压缩与解压模块
Python Gzip模块为GNU zip文件提供了一个类文件的接口,它使用zlib来压缩和解压缩数据文件,读写gzip文件。
首先,我们来看一下压缩与解压的流程代码:
一、使用gzip模块压缩文件
>>> import gzip #导入python gzip模块,玩蛇网提示:注意名字为全小写
>>> g = gzip.GzipFile(filename="", mode="wb", compresslevel=9, fileobj=open('sitemap.log.gz', 'wb'))
>>> g.write(open('d:\\test\\sitemap.xml').read())
>>> g.close()
其中,filename参数是压缩文件内,文件的名字,为空也可以,不修改。fileobj是生成的压缩文件对象,它的路径名称等。最后是把文件写入gzip文件中去,再关闭操作连接。
二、使用gzip模块解压缩文件
>>> g = gzip.GzipFile(mode="rb", fileobj=open('d:\\test\\sitemap.log.gz', 'rb')) # python gzip 解压
>>> open(r"d:\\haha.xml", "wb").write(g.read())
使用的时候注意,函数方法的大小写一点要看仔细,如果gzip文件是这种形式的:*.tar.gz,证明先是由tar命令压缩后,后再由 gzip压缩的,需要先用解压缩tar文件,再用gzip模块解压缩。其实,现在很多网页为了提高浏览器端用户的访问速度,和搜索引擎爬虫抓取的速度,都在使用gzip压缩。
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/module/gzip.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
PYTHON基础入门必备教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
PYTHON进阶提高必备教程 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python linecache模块缓存读取大文件指定行
- • Python OS模块常用功能 中文图文详解
- • Python json解析模块loads/dumps中文encode教程
- • Python random模块sample、randint、shuffle、cho
- • Python urllib2模块post/get 下载网络资源
- • Python re正则表达式操作指南
- • Python time模块 函数格式 时间操作源码演示
- • Python Gzip压缩与解压模块
- • 第三方时间日期库 Python Arrow模块
- • Python Queue模块 多线程安全 先进先出的实
图文精华 RECOMMEND
-
Python linecache模块缓存读取大文件
-
Python OS模块常用功能 中文图文详
-
Python json解析模块loads/dumps中文e
-
Python random模块sample、randint、shu
-
Python re正则表达式操作指南
-
Python time模块 函数格式 时间操作
热点文章 HOT
- Python json解析模块loads/dumps中文encode教程
- Python re正则表达式操作指南
- Python threading多线程模块
- Python string模块 字符串函数方法操作教程
- Python robotparser 网络蜘蛛robots.txt搜索访问
- Python Cookie HTTP获取cookie并处理
- Python urllib模块 网络资源访问安装下载
- Python csv模块读写中文乱码等问题解决
- Python urllib2模块post/get 下载网络资源
- Python sys模块 argv path常用方法图文详解