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处理中文编码和判断编码示例
Feb 26 Python
python格式化字符串实例总结
Sep 28 Python
Python实现导出数据生成excel报表的方法示例
Jul 12 Python
几种实用的pythonic语法实例代码
Feb 24 Python
python实现按长宽比缩放图片
Jun 07 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
PyQt5 实现字体大小自适应分辨率的方法
Jun 18 Python
python生成大写32位uuid代码
Mar 03 Python
Python实现aes加密解密多种方法解析
May 15 Python
Python子进程subpocess原理及用法解析
Jul 16 Python
Python中的With语句的使用及原理
Jul 29 Python
Python3读写ini配置文件的示例
Nov 06 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
自制汽车收音机天线:收听广播的技巧和方法
2021/03/02 无线电
PHP编程过程中需要了解的this,self,parent的区别
2009/12/30 PHP
PHP超级全局变量数组小结
2012/10/04 PHP
php自动获取关键字的方法
2015/01/06 PHP
PHP多种序列化/反序列化的方法详解
2017/06/23 PHP
PDO::rollBack讲解
2019/01/29 PHP
php报错502badgateway解决方法
2019/10/11 PHP
PHP实现创建一个RPC服务操作示例
2020/02/23 PHP
js导出table到excel同时兼容FF和IE示例
2013/09/03 Javascript
基于javascript实现窗口抖动效果
2016/01/03 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
2016/04/01 Javascript
vue-axios使用详解
2017/05/10 Javascript
JS简单实现父子窗口传值功能示例【未使用iframe框架】
2017/09/20 Javascript
微信小程序收藏功能的实现代码
2018/06/12 Javascript
基于纯JS实现多张图片的懒加载Lazy过程解析
2019/10/14 Javascript
浅谈Vue为什么不能检测数组变动
2019/10/14 Javascript
JSON获取属性值方法代码实例
2020/06/30 Javascript
vue 实现tab切换保持数据状态
2020/07/21 Javascript
[49:08]OpTic vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python实现给字典添加条目的方法
2014/09/25 Python
python不换行之end=与逗号的意思及用途
2017/11/21 Python
Python中整数的缓存机制讲解
2019/02/16 Python
python多线程并发实例及其优化
2019/06/27 Python
python基于pdfminer库提取pdf文字代码实例
2019/08/15 Python
详解基于python的多张不同宽高图片拼接成大图
2019/09/26 Python
matplotlib制作雷达图报错ValueError的实现
2021/01/05 Python
数据库面试要点基本概念
2013/10/31 面试题
餐饮业会计岗位职责
2013/12/19 职场文书
幼儿园国庆节活动方案
2014/02/01 职场文书
《珍珠泉》教学反思
2014/02/20 职场文书
市场营销方案范文
2014/03/11 职场文书
小学生我的梦想演讲稿
2014/08/21 职场文书
师德师风自我评价范文
2014/09/11 职场文书
单位收入证明范本
2015/06/18 职场文书
奖学金申请书(范文)
2019/08/14 职场文书
Python进度条的使用
2021/05/17 Python