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 相关文章推荐
Django框架中处理URLconf中特定的URL的方法
Jul 20 Python
玩转python selenium鼠标键盘操作(ActionChains)
Apr 12 Python
ansible作为python模块库使用的方法实例
Jan 17 Python
Python做简单的字符串匹配详解
Mar 21 Python
Selenium chrome配置代理Python版的方法
Nov 29 Python
用python脚本24小时刷浏览器的访问量方法
Dec 07 Python
python根据文章标题内容自动生成摘要的实例
Feb 21 Python
详解Python sys.argv使用方法
May 10 Python
python字典的遍历3种方法详解
Aug 10 Python
django中的图片验证码功能
Sep 18 Python
Keras 快速解决OOM超内存的问题
Jun 11 Python
python爬虫中的url下载器用法详解
Nov 30 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
ThinkPHP3.1新特性之对分组支持的改进与完善概述
2014/06/19 PHP
php获取服务器操作系统相关信息的方法
2016/10/08 PHP
dojo 之基础篇
2007/03/24 Javascript
用js生产批量批处理执行命令
2008/07/28 Javascript
javascript 快速排序函数代码
2012/05/30 Javascript
jquery.qrcode在线生成二维码使用示例
2013/08/21 Javascript
用Javascript获取页面元素的具体位置
2013/12/09 Javascript
给html超链接设置事件不使用href来完成跳
2014/04/20 Javascript
JQuery插件ajaxfileupload.js异步上传文件实例
2015/05/19 Javascript
Javascript 制作图形验证码实例详解
2016/12/22 Javascript
微信小程序 http请求的session管理
2017/06/07 Javascript
vue resource post请求时遇到的坑
2017/10/19 Javascript
vue 中滚动条始终定位在底部的方法
2018/09/03 Javascript
深入理解Puppeteer的入门教程和实践
2019/03/05 Javascript
layui switch 开关监听 弹出确定状态转换的例子
2019/09/21 Javascript
layui表格设计以及数据初始化详解
2019/10/26 Javascript
Python实现抢购IPhone手机
2018/02/07 Python
Python实现数值积分方式
2019/11/20 Python
python pyqtgraph 保存图片到本地的实例
2020/03/14 Python
Python3内置函数chr和ord实现进制转换
2020/06/05 Python
python json.dumps() json.dump()的区别详解
2020/07/14 Python
HTML5标签嵌套规则详解【必看】
2016/04/26 HTML / CSS
Html5之webcoekt播放JPEG图片流
2020/09/22 HTML / CSS
Keds官方网站:购买帆布运动鞋和经典皮鞋
2016/11/12 全球购物
Sunglasses Shop丹麦:欧洲第一的太阳镜在线销售网站
2017/10/22 全球购物
意大利在线药房:Saninforma
2021/02/11 全球购物
计算机专业自我鉴定
2013/10/15 职场文书
函授毕业生自我鉴定
2013/11/06 职场文书
班队活动设计方案
2014/01/30 职场文书
医学专业大学生职业生涯规划书
2014/10/25 职场文书
党员违纪检讨书怎么写
2014/11/01 职场文书
2016年过年放假安排通知
2015/08/18 职场文书
2015年度工程师评职称工作总结
2015/10/14 职场文书
2016年端午节红领巾广播稿
2015/12/18 职场文书
《青山不老》教学反思
2016/02/22 职场文书
Python中快速掌握Data Frame的常用操作
2021/03/31 Python