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中requests和https使用简单示例
Jan 18 Python
Python smtplib实现发送邮件功能
May 22 Python
pandas分别写入excel的不同sheet方法
Dec 11 Python
Python中捕获键盘的方式详解
Mar 28 Python
Python字符串匹配之6种方法的使用详解
Apr 08 Python
Python3.5面向对象程序设计之类的继承和多态详解
Apr 24 Python
django表单的Widgets使用详解
Jul 22 Python
Django在admin后台集成TinyMCE富文本编辑器的例子
Aug 09 Python
Pandas 缺失数据处理的实现
Nov 04 Python
selenium 多窗口切换的实现(windows)
Jan 18 Python
Python @property原理解析和用法实例
Feb 11 Python
python 深度学习中的4种激活函数
Sep 18 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生成随机密码的三种方法小结
2010/09/04 PHP
PHP检测移动设备类mobile detection使用实例
2014/04/14 PHP
简单的php+mysql聊天室实现方法(附源码)
2016/01/05 PHP
AJAX的使用方法详解
2017/04/29 PHP
gearman中任务的优先级和返回状态实例分析
2020/02/27 PHP
js中字符替换函数String.replace()使用技巧
2011/08/14 Javascript
JavaScript NaN和Infinity特殊值 [译]
2012/09/20 Javascript
javascript获取网页中指定节点的父节点、子节点的方法小结
2013/04/24 Javascript
window.navigate 与 window.location.href 的使用区别介绍
2013/09/21 Javascript
初识Node.js
2015/03/20 Javascript
基于jQuery实现点击弹出层实例代码
2016/01/01 Javascript
jQuery实现每隔几条元素增加1条线的方法
2016/06/27 Javascript
mint-ui的search组件在键盘显示搜索按钮的实现方法
2017/10/27 Javascript
微信小程序有旋转动画效果的音乐组件实例代码
2018/08/22 Javascript
vue3.0 CLI - 1 - npm 安装与初始化的入门教程
2018/09/14 Javascript
js实现删除li标签一行内容
2019/04/16 Javascript
[01:08:57]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS LGD第二场
2014/05/24 DOTA
分享6个隐藏的python功能
2017/12/07 Python
python实现超简单的视频对象提取功能
2018/06/04 Python
python 寻找list中最大元素对应的索引方法
2018/06/28 Python
python3利用venv配置虚拟环境及过程中的小问题小结
2018/08/01 Python
解决python文件双击运行秒退的问题
2019/06/24 Python
python tkiner实现 一个小小的图片翻页功能的示例代码
2020/06/24 Python
Python2及Python3如何实现兼容切换
2020/09/01 Python
基于Python 函数和方法的区别说明
2021/03/24 Python
简短的公司员工自我评价分享
2013/11/13 职场文书
运动会广播稿50字
2014/01/26 职场文书
小学校园活动策划
2014/01/30 职场文书
国际贸易专业个人鉴定
2014/02/22 职场文书
餐饮总经理岗位职责
2014/03/07 职场文书
企业安全生产责任书
2014/04/14 职场文书
经济贸易专业自荐信
2014/06/11 职场文书
刑事辩护授权委托书
2014/09/13 职场文书
道德与公民自我评价
2015/03/09 职场文书
Apache Pulsar结合Hudi构建Lakehouse方案分析
2022/03/31 Servers
MySQL磁盘碎片整理实例演示
2022/04/03 MySQL