python中zip和unzip数据的方法


Posted in Python onMay 27, 2015

本文实例讲述了python zip和unzip数据的方法。分享给大家供大家参考。具体实现方法如下:

# zipping and unzipping a string using the zlib module
# a very large string could be zipped and saved to a file speeding up file writing time 
# and later reloaded and unzipped by another program speeding up reading of the file
# tested with Python24   vegaseat   15aug2005
import zlib
str1 = \
"""Dallas Cowboys football practice at Valley Ranch was delayed on Wednesday 
for nearly two hours. One of the players, while on his way to the locker
room happened to look down and notice a suspicious looking, unknown white
powdery substance on the practice field.
The coaching staff immediately suspended practice while the FBI was
called in to investigate. After a complete field analysis, the FBI
determined that the white substance unknown to the players was the goal
line.
Practice was resumed when FBI Special Agents decided that the team would not
be likely to encounter the substance again.
"""
print '-'*70 # 70 dashes for the fun of it
print str1
print '-'*70
crc_check1 = zlib.crc32(str1)
print "crc before zip=", crc_check1
print "Length of original str1 =", len(str1)
# zip compress the string
zstr1 = zlib.compress(str1)
print "Length of zipped str1 =", len(zstr1)
filename = 'Dallas.zap'
# write the zipped string to a file
fout = open(filename, 'w')
try:
  print >> fout, zstr1
except IOError:
  print "Failed to open file..."
else:
  print "done writing", filename
fout.close()
# read the zip file back
fin = open(filename, 'r')
try:
  zstr2 = fin.read()
except IOError:
  print "Failed to open file..."
else:
  print "done reading", filename
fin.close()
# unzip the zipped string from the file
str2 = zlib.decompress(zstr2)
print '-'*70
print str2
print '-'*70
crc_check2 = zlib.crc32(str2)
print "crc after unzip =", crc_check2, "(check sums should match)"

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
详细解读Python中的__init__()方法
May 02 Python
简单讲解Python编程中namedtuple类的用法
Jun 21 Python
Linux下为不同版本python安装第三方库
Aug 31 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
Apr 24 Python
关于python的list相关知识(推荐)
Aug 30 Python
python 地图经纬度转换、纠偏的实例代码
Aug 06 Python
python实现名片管理系统项目
Apr 26 Python
python tools实现视频的每一帧提取并保存
Mar 20 Python
Pytorch 实现自定义参数层的例子
Aug 17 Python
python Dijkstra算法实现最短路径问题的方法
Sep 19 Python
pandas分批读取大数据集教程
Jun 06 Python
Python datetime 如何处理时区信息
Sep 02 Python
Python pickle模块用法实例分析
May 27 #Python
Python创建模块及模块导入的方法
May 27 #Python
Python类的用法实例浅析
May 27 #Python
Python socket编程实例详解
May 27 #Python
Python简单删除目录下文件以及文件夹的方法
May 27 #Python
python解析xml文件实例分析
May 27 #Python
Python定时执行之Timer用法示例
May 27 #Python
You might like
咖啡知识 除了喝咖啡还有那些知识点
2021/03/06 新手入门
用PHP实现文件上传二法
2006/10/09 PHP
php 取得瑞年与平年的天数的代码
2009/08/10 PHP
解析PHP多种序列化与反序列化的方法
2013/06/06 PHP
PHP实现生成数据字典功能示例
2018/05/24 PHP
PHP操作redis实现的分页列表,新增,删除功能封装类与用法示例
2018/08/04 PHP
MacOS下PHP7.1升级到PHP7.4.15的方法
2021/02/22 PHP
jQuery修改class属性和CSS样式整理
2015/01/30 Javascript
JS+CSS实现的蓝色table选项卡效果
2015/10/08 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
BootStrap iCheck插件全选与获取value值的解决方法
2016/08/24 Javascript
微信小程序  自定义创建详细介绍
2016/10/27 Javascript
jQuery输入框密码的显示隐藏【代码分享】
2017/04/29 jQuery
JavaScript生成简单等差数列
2017/11/28 Javascript
Node.JS段点续传:Nginx配置文件分段下载功能的实现方法
2018/03/12 Javascript
详解webpack打包时排除其中一个css、js文件或单独打包一个css、js文件(两种方法)
2018/10/26 Javascript
vue实现分页组件
2020/06/16 Javascript
vue中h5端打开app(判断是安卓还是苹果)
2021/02/26 Vue.js
Python常用的爬虫技巧总结
2016/03/28 Python
Python实现的异步代理爬虫及代理池
2017/03/17 Python
python数据类型_元组、字典常用操作方法(介绍)
2017/05/30 Python
pandas中的DataFrame按指定顺序输出所有列的方法
2018/04/10 Python
python调用摄像头显示图像的实例
2018/08/03 Python
从列表或字典创建Pandas的DataFrame对象的方法
2019/07/06 Python
python 如何在测试中使用 Mock
2021/03/01 Python
关于css中margin的值和垂直外边距重叠问题
2020/10/27 HTML / CSS
荷兰皇家航空公司官方网站:KLM Royal Dutch Airlines
2017/12/07 全球购物
行政助理岗位职责范文
2013/12/03 职场文书
部门活动策划方案
2014/08/16 职场文书
支部书记四风对照材料
2014/08/28 职场文书
营销与策划实训报告
2014/11/05 职场文书
2015年招聘工作总结
2014/12/12 职场文书
小学德育工作总结2015
2015/05/12 职场文书
Python 语言实现六大查找算法
2021/06/30 Python
HTML+CSS实现导航条下拉菜单的示例代码
2021/08/02 HTML / CSS
面试提问mysql一张表到底能存多少数据
2022/03/13 MySQL