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 示例分享---逻辑推理编程解决八皇后
Jul 20 Python
python写日志封装类实例
Jun 28 Python
python万年历实现代码 含运行结果
May 20 Python
Python实现随机生成有效手机号码及身份证功能示例
Jun 05 Python
python基础while循环及if判断的实例讲解
Aug 25 Python
django用户登录和注销的实现方法
Jul 16 Python
Python中模块(Module)和包(Package)的区别详解
Aug 07 Python
python读写csv文件的方法
Aug 13 Python
Python生成器实现简单"生产者消费者"模型代码实例
Mar 27 Python
基于Python3读写INI配置文件过程解析
Jul 23 Python
Python实现王者荣耀自动刷金币的完整步骤
Jan 22 Python
Flask response响应的具体使用
Jul 15 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
获得Google PR值的PHP代码
2007/01/28 PHP
php 中英文语言转换类
2011/09/07 PHP
PHP mb_convert_encoding文字编码的转换函数介绍
2011/11/10 PHP
整理的一些实用WordPress后台MySQL操作命令
2013/01/07 PHP
php验证手机号码
2015/11/11 PHP
PHP单例模式实例分析【防继承,防克隆操作】
2019/05/22 PHP
PHP使用PDO实现mysql防注入功能详解
2019/12/20 PHP
JavaScript中的isXX系列是否继续使用的分析
2011/04/16 Javascript
分享几种比较简单实用的JavaScript tabel切换
2015/12/31 Javascript
Angular2中Bootstrap界面库ng-bootstrap详解
2016/10/18 Javascript
node.js中的事件处理机制详解
2016/11/26 Javascript
jQuery实现导航回弹效果
2017/02/27 Javascript
详谈vue+webpack解决css引用图片打包后找不到资源文件的问题
2018/03/06 Javascript
[01:11]辉夜杯战队访谈宣传片—CDEC.Y
2015/12/26 DOTA
[40:03]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#1EHOME VS Archon
2016/03/02 DOTA
[57:55]EG vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
python简单猜数游戏实例
2015/07/09 Python
python中pandas.DataFrame排除特定行方法示例
2017/03/12 Python
利用python写个下载teahour音频的小脚本
2017/05/08 Python
python实现文件的备份流程详解
2019/06/18 Python
python 实现多维数组转向量
2019/11/30 Python
python实现全排列代码(回溯、深度优先搜索)
2020/02/26 Python
python GUI库图形界面开发之PyQt5状态栏控件QStatusBar详细使用方法实例
2020/02/28 Python
新手学习Python2和Python3中print不同的用法
2020/06/09 Python
浅谈Python 函数式编程
2020/06/20 Python
css3 边框、背景、文本效果的实现代码
2018/03/21 HTML / CSS
美国眼镜网站:EyeBuyDirect
2017/04/13 全球购物
Casetify官网:自制专属手机壳、iPad护壳和Apple Watch手表带
2018/05/09 全球购物
Flesh Beauty官网:露华浓集团旗下彩妆品牌
2021/02/15 全球购物
精彩的大学生自我评价
2013/11/17 职场文书
最新结婚典礼主持词
2014/03/14 职场文书
工会主席事迹材料
2014/06/03 职场文书
学校推普周活动总结
2015/05/07 职场文书
火烧圆明园观后感
2015/06/03 职场文书
2016会计专业自荐信范文
2016/01/28 职场文书
mysql自增长id用完了该怎么办
2022/02/12 MySQL