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模拟登录12306的方法
Dec 30 Python
Python聊天室实例程序分享
Jan 05 Python
python3使用requests模块爬取页面内容的实战演练
Sep 25 Python
Python实现PS图像调整黑白效果示例
Jan 25 Python
关于Tensorflow中的tf.train.batch函数的使用
Apr 24 Python
python实现字符串和字典的转换
Sep 29 Python
python re库的正则表达式入门学习教程
Mar 08 Python
PyQt5 QTable插入图片并动态更新的实例
Jun 18 Python
在pytorch中为Module和Tensor指定GPU的例子
Aug 19 Python
python实现从wind导入数据
Dec 03 Python
Django Haystack 全文检索与关键词高亮的实现
Feb 17 Python
python 使用tkinter与messagebox写界面和弹窗
Mar 20 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
在php和MySql中计算时间差的方法详解
2015/03/27 PHP
CI映射(加载)数据到view层的方法
2016/03/28 PHP
js技巧--转义符"\"的妙用
2007/01/09 Javascript
让textarea控件的滚动条怎是位与最下方
2007/04/20 Javascript
php实例分享之实现显示网站运行时间
2014/05/20 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
2016/04/17 Javascript
JS递归遍历对象获得Value值方法技巧
2016/06/14 Javascript
浅述Javascript的外部对象
2016/12/07 Javascript
Vue 表单控件绑定的实现示例
2017/08/11 Javascript
JS模拟超市简易收银台小程序代码解析
2017/08/18 Javascript
Node.js实现注册邮箱激活功能的方法示例
2018/03/23 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
VUE注册全局组件和局部组件过程解析
2019/10/10 Javascript
python 打印对象的所有属性值的方法
2016/09/11 Python
用Python登录好友QQ空间点赞的示例代码
2017/11/04 Python
浅谈Django自定义模板标签template_tags的用处
2017/12/20 Python
python 文件转成16进制数组的实例
2018/07/09 Python
python判断字符串或者集合是否为空的实例
2019/01/23 Python
python修改FTP服务器上的文件名
2019/09/11 Python
Pytorch中的variable, tensor与numpy相互转化的方法
2019/10/10 Python
pygame实现俄罗斯方块游戏(基础篇1)
2019/10/29 Python
python 用pandas实现数据透视表功能
2020/12/21 Python
html5 canvas 简单画板实现代码
2012/01/05 HTML / CSS
美国最大的网络男装服装品牌:Bonobos
2017/05/25 全球购物
美津浓巴西官方网站:Mizuno巴西
2019/07/24 全球购物
德国最新街头服饰网上商店:BODYCHECK
2019/09/15 全球购物
武汉高蓝德国际.net机试
2016/06/24 面试题
大学生职业生涯规划书模板
2014/01/18 职场文书
甜美蛋糕店创业计划书
2014/01/30 职场文书
双拥工作宣传标语
2014/06/26 职场文书
作风年建设汇报材料
2014/08/14 职场文书
运动会广播稿100字
2014/09/14 职场文书
关于工作时间玩手机的检讨书
2014/09/18 职场文书
公司党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
运动会广播稿50字
2015/08/19 职场文书
投资入股协议书
2016/03/22 职场文书