python中zip和unzip數(shù)據(jù)的方法
更新時間:2015年05月27日 12:42:52 作者:依山帶水
這篇文章主要介紹了python中zip和unzip數(shù)據(jù)的方法,實例分析了Python中zlib模塊的相關(guān)使用技巧,需要的朋友可以參考下
本文實例講述了python zip和unzip數(shù)據(jù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
# 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程序設(shè)計有所幫助。
相關(guān)文章
Python使用OPENCV的目標(biāo)跟蹤算法實現(xiàn)自動視頻標(biāo)注效果
這篇文章主要介紹了Python使用OPENCV的目標(biāo)跟蹤算法進行簡單的自動視頻標(biāo)注,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09python如何給字典的鍵對應(yīng)的值為字典項的字典賦值
這篇文章主要介紹了python如何給字典的鍵對應(yīng)的值為字典項的字典賦值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07在Django中創(chuàng)建URLconf相關(guān)的通用視圖的方法
這篇文章主要介紹了在Django中創(chuàng)建URLconf相關(guān)的通用視圖的方法,Django是Python重多人氣框架中最為著名的一個,需要的朋友可以參考下2015-07-07Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
讀萬卷書不如行萬里路,學(xué)的扎不扎實要通過實戰(zhàn)才能看出來,今天小編給大家?guī)硪粋€python爬蟲案例,采集回車桌面網(wǎng)站的美女圖片,大家可以在過程中查缺補漏,看看自己掌握程度怎么樣2021-10-10使用python編寫批量卸載手機中安裝的android應(yīng)用腳本
該腳本的功能是卸載android手機中安裝的所有第三方應(yīng)用,主要是使用adb shell pm、adb uninstall 命令,需要的朋友可以參考下2014-07-07