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多线程操作实例
Nov 21 Python
简单分析Python中用fork()函数生成的子进程
May 04 Python
Python中字典的基础知识归纳小结
Aug 19 Python
Python实现生成随机数据插入mysql数据库的方法
Dec 25 Python
为什么选择python编程语言入门黑客攻防 给你几个理由!
Feb 02 Python
浅析Python 引号、注释、字符串
Jul 25 Python
Python实现性能自动化测试竟然如此简单
Jul 30 Python
pandas按行按列遍历Dataframe的几种方式
Oct 23 Python
sklearn+python:线性回归案例
Feb 24 Python
Python2.6版本pip安装步骤解析
Aug 17 Python
OpenCV利用python来实现图像的直方图均衡化
Oct 21 Python
python 标准库原理与用法详解之os.path篇
Oct 24 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笔记之:文章中图片处理的使用
2013/04/26 PHP
PHP5.2中PDO的简单使用方法
2016/03/25 PHP
PHP微信公众号开发之微信红包实现方法分析
2017/07/14 PHP
Javascript SHA-1:Secure Hash Algorithm
2006/12/20 Javascript
javascript 动态加载 css 方法总结
2009/07/11 Javascript
jQuery 使用手册(六)
2009/09/23 Javascript
select 控制网页内容隐藏于显示的实现代码
2010/05/25 Javascript
js 处理URL实用技巧
2010/11/23 Javascript
jquery load()在firefox(火狐)下显示不正常的解决方法
2011/04/05 Javascript
jQuery表格插件datatables用法详解
2020/11/23 Javascript
基于jQuery的Web上传插件Uploadify使用示例
2016/05/19 Javascript
JS使用正则截取两个字符串之间的字符串实现方法详解
2017/01/06 Javascript
Angularjs中的ui-bootstrap的使用教程
2017/02/19 Javascript
使用Math.max,Math.min获取数组中的最值实例
2017/04/25 Javascript
JavaScript for循环 if判断语句(学习笔记)
2017/10/11 Javascript
vue 标签属性数据绑定和拼接的实现方法
2018/05/17 Javascript
解决vue-cli单页面手机应用input点击手机端虚拟键盘弹出盖住input问题
2018/08/25 Javascript
vue.js实现带日期星期的数字时钟功能示例
2018/08/28 Javascript
详解vue如何使用rules对表单字段进行校验
2018/10/17 Javascript
详解iview的checkbox多选框全选时校验问题
2019/06/10 Javascript
最简单的vue消息提示全局组件的方法
2019/06/16 Javascript
微信小程序实现左滑动删除效果
2020/03/30 Javascript
React实现评论的添加和删除
2020/10/20 Javascript
详解vue中在父组件点击按钮触发子组件的事件
2020/11/13 Javascript
node koa2 ssr项目搭建的方法步骤
2020/12/11 Javascript
[40:27]完美世界DOTA2联赛PWL S3 PXG vs GXR 第一场 12.19
2020/12/24 DOTA
用python代码做configure文件
2014/07/20 Python
python 实现求解字符串集的最长公共前缀方法
2018/07/20 Python
如何在Django中设置定时任务的方法示例
2019/01/18 Python
使用python从三个角度解决josephus问题的方法
2020/03/27 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
2020/07/15 Python
党的群众教育实践活动实施方案
2014/06/12 职场文书
党员四风自我剖析材料思想汇报
2014/09/13 职场文书
微笑面对生活演讲稿
2014/09/23 职场文书
SqlServer数据库远程连接案例教程
2021/07/15 SQL Server
python数据可视化JupyterLab实用扩展程序Mito
2021/11/20 Python