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程序設計有所幫助。
相關文章
Python使用OPENCV的目標跟蹤算法實現自動視頻標注效果
這篇文章主要介紹了Python使用OPENCV的目標跟蹤算法進行簡單的自動視頻標注,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
在Django中創(chuàng)建URLconf相關的通用視圖的方法
這篇文章主要介紹了在Django中創(chuàng)建URLconf相關的通用視圖的方法,Django是Python重多人氣框架中最為著名的一個,需要的朋友可以參考下2015-07-07
使用python編寫批量卸載手機中安裝的android應用腳本
該腳本的功能是卸載android手機中安裝的所有第三方應用,主要是使用adb shell pm、adb uninstall 命令,需要的朋友可以參考下2014-07-07

