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中处理出错页面的方法
Jul 15 Python
Python中函数及默认参数的定义与调用操作实例分析
Jul 25 Python
python3+PyQt5实现自定义流体混合窗口部件
Apr 24 Python
python将txt等文件中的数据读为numpy数组的方法
Dec 22 Python
学习python的前途 python挣钱
Feb 27 Python
Python 控制终端输出文字的实例
Jul 12 Python
FFrpc python客户端lib使用解析
Aug 24 Python
python 下 CMake 安装配置 OPENCV 4.1.1的方法
Sep 30 Python
Python3 A*寻路算法实现方式
Dec 24 Python
解析PyCharm Python运行权限问题
Jan 08 Python
在Mac中PyCharm配置python Anaconda环境过程图解
Mar 11 Python
Python selenium绕过webdriver监测执行javascript
Apr 12 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
深入apache host的配置详解
2013/06/09 PHP
php通过获取头信息判断图片类型的方法
2015/06/26 PHP
Codeigniter控制器controller继承问题实例分析
2016/01/19 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
2020/03/26 PHP
JavaScript 在各个浏览器中执行的耐性
2009/04/06 Javascript
JQuery 返回布尔值Is()条件判断方法代码
2012/05/14 Javascript
jquery mobile页面跳转后样式丢失js失效的解决方法
2014/09/06 Javascript
JavaScript错误处理
2015/02/03 Javascript
基于jquery实现一个滚动的分步注册向导-附源码
2015/08/26 Javascript
jQuery Validate初步体验(一)
2015/12/12 Javascript
Javascript数组Array方法解读
2016/03/13 Javascript
js将json格式的对象拼接成复杂的url参数方法
2016/05/25 Javascript
JS模拟bootstrap下拉菜单效果实例
2016/06/17 Javascript
JavaScript 对象详细整理总结
2016/09/29 Javascript
jsonp跨域请求实现示例
2017/03/13 Javascript
Vue动态生成el-checkbox点击无法赋值的解决方法
2019/02/21 Javascript
Python中使用Tkinter模块创建GUI程序实例
2015/01/14 Python
Python实现中文数字转换为阿拉伯数字的方法示例
2017/05/26 Python
Django实现组合搜索的方法示例
2018/01/23 Python
Python OpenCV获取视频的方法
2018/02/28 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
python实现对图片进行旋转,放缩,裁剪的功能
2019/08/07 Python
pytorch AvgPool2d函数使用详解
2020/01/03 Python
Python实现ATM系统
2020/02/17 Python
利用Python自动化操作AutoCAD的实现
2020/04/01 Python
Python使用urlretrieve实现直接远程下载图片的示例代码
2020/08/17 Python
关于HTML5语义标签的实践(blog页面)
2016/07/12 HTML / CSS
在线购买廉价折扣书籍和小说:BookOutlet.com
2018/02/19 全球购物
介绍一下RMI的基本概念
2016/12/17 面试题
药学专业大学生自荐信
2013/09/28 职场文书
企业法人授权委托书
2014/09/25 职场文书
查摆剖析材料范文
2014/09/30 职场文书
MySQL sql_mode修改不生效的原因及解决
2021/05/07 MySQL
Python Pycharm虚拟下百度飞浆PaddleX安装报错问题及处理方法(亲测100%有效)
2021/05/24 Python
Python 数据结构之十大经典排序算法一文通关
2021/10/16 Python
「魔导具师妲莉亚永不妥协~从今天开始的自由职人生活~」1、2卷发售宣传CM公开
2022/03/21 日漫