python中zip和unzip数据的方法

 更新时间:2015年05月27日 12:42:52   作者:依山带水  
这篇文章主要介绍了python中zip和unzip数据的方法,实例分析了Python中zlib模块的相关使用技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • python3 selenium自动化测试 强大的CSS定位方法

    python3 selenium自动化测试 强大的CSS定位方法

    今天小编就为大家分享一篇python3 selenium自动化测试 强大的CSS定位方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • 基于Python实现本地音乐播放器的制作

    基于Python实现本地音乐播放器的制作

    这篇文章主要介绍了如何利用Python实现本地音乐播放器的制作,并且可以选择需要播放的音乐的路径,选择播放方式,感兴趣的小伙伴可以了解一下
    2022-06-06
  • Python爬虫之使用BeautifulSoup和Requests抓取网页数据

    Python爬虫之使用BeautifulSoup和Requests抓取网页数据

    这篇文章主要介绍了Python爬虫之使用BeautifulSoup和Requests抓取网页数据,本篇文章将介绍如何使用 Python 编写一个简单的网络爬虫,从网页中提取有用的数据,需要的朋友可以参考下
    2023-04-04
  • python实现数独游戏 java简单实现数独游戏

    python实现数独游戏 java简单实现数独游戏

    这篇文章主要为大家详细介绍了python实现数独游戏和java实现数独游戏的相关代码,比较两种语言实现数独游戏的区别
    2018-03-03
  • python名片管理系统开发

    python名片管理系统开发

    这篇文章主要为大家详细介绍了python名片管理系统开发,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • Python采集天天基金数据掌握最新基金动向

    Python采集天天基金数据掌握最新基金动向

    这篇文章主要介绍了Python采集天天基金数据掌握最新基金动向,本次案例实现流程为发送请求、获取数据、解析数据、多页爬取、保存数据,接下来来看看具体的操作过程吧
    2022-01-01
  • 浅析对torch.unsqueeze()函数理解

    浅析对torch.unsqueeze()函数理解

    torch.unsqueeze()函数起到升维的作用,dim等于几表示在第几维度加一,这篇文章主要介绍了对torch.unsqueeze()函数理解深度解析,感兴趣的朋友跟随小编一起看看吧
    2024-06-06
  • Python实现处理apiDoc转swagger的方法详解

    Python实现处理apiDoc转swagger的方法详解

    这篇文章主要为大家详细介绍了Python实现处理apiDoc转swagger的方法,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-02-02
  • python 发送和接收ActiveMQ消息的实例

    python 发送和接收ActiveMQ消息的实例

    今天小编就为大家分享一篇python 发送和接收ActiveMQ消息的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 解决Python发送Http请求时,中文乱码的问题

    解决Python发送Http请求时,中文乱码的问题

    这篇文章主要介绍了解决Python发送Http请求时,中文乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论