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进阶教程之异常处理
Aug 30 Python
Python最长公共子串算法实例
Mar 07 Python
python获得linux下所有挂载点(mount points)的方法
Apr 29 Python
Python脚本处理空格的方法
Aug 08 Python
Python正则表达式匹配中文用法示例
Jan 17 Python
python中MethodType方法介绍与使用示例
Aug 03 Python
Python对CSV、Excel、txt、dat文件的处理
Sep 18 Python
解决安装pycharm后不能执行python脚本的问题
Jan 19 Python
关于python中密码加盐的学习体会小结
Jul 15 Python
Python响应对象text属性乱码解决方案
Mar 31 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
Apr 02 Python
python实现录音功能(可随时停止录音)
Oct 26 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
《超神学院》霸气归来, 天使彦上演维多利亚的秘密
2020/03/02 国漫
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
Laravel 简单实现Ajax滚动加载示例
2019/10/22 PHP
jQuery中RadioButtonList的功能及用法实例介绍
2013/08/23 Javascript
jQuery表单域属性过滤器用法分析
2015/02/10 Javascript
javascript原始值和对象引用实例分析
2015/04/25 Javascript
Nodejs中 npm常用命令详解
2016/07/04 NodeJs
AngularJS基础 ng-include 指令示例讲解
2016/08/01 Javascript
js仿手机页面文件下拉刷新效果
2016/10/14 Javascript
JS条形码(一维码)插件JsBarcode用法详解【编码类型、参数、属性】
2017/04/19 Javascript
移动端使用localResizeIMG4压缩图片
2017/04/22 Javascript
BootStrap Table复选框默认选中功能的实现代码(从数据库获取到对应的状态进行判断是否为选中状态)
2017/07/11 Javascript
javaScript强制保留两位小数的输入数校验和小数保留问题
2018/05/09 Javascript
JavaScript中常见内置函数用法示例
2018/05/14 Javascript
详解Vue源码学习之callHook钩子函数
2018/07/25 Javascript
vue.js图片转Base64上传图片并预览的实现方法
2018/08/02 Javascript
Vue实现动态添加或者删除对象和对象数组的操作方法
2018/09/21 Javascript
vue返回上一页面时回到原先滚动的位置的方法
2018/12/20 Javascript
Vue在chrome44偶现点击子元素事件无法冒泡的解决方法
2019/12/15 Javascript
微信小程序学习总结(一)项目创建与目录结构分析
2020/06/04 Javascript
JavaScript读取本地文件常用方法流程解析
2020/10/12 Javascript
python批量导出导入MySQL用户的方法
2013/11/15 Python
使用Python生成XML的方法实例
2017/03/21 Python
python基础while循环及if判断的实例讲解
2017/08/25 Python
python查询mysql,返回json的实例
2018/03/26 Python
Python多继承顺序实例分析
2018/05/26 Python
Python中交换两个元素的实现方法
2018/06/29 Python
Python拆分大型CSV文件代码实例
2019/10/07 Python
python3实现弹弹球小游戏
2019/11/25 Python
Python爬虫:Request Payload和Form Data的简单区别说明
2020/04/30 Python
美国马匹用品和骑马配件购物网站:Horse.com
2018/01/08 全球购物
当文件系统受到破坏时,如何检查和修复系统?
2012/03/09 面试题
工厂门卫的岗位职责
2014/07/27 职场文书
节能环保家庭事迹材料
2014/08/27 职场文书
初中国旗下的演讲稿
2014/08/28 职场文书
担保书怎么写 ?
2019/04/22 职场文书