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)文章
python3 selenium自動化測試 強大的CSS定位方法
今天小編就為大家分享一篇python3 selenium自動化測試 強大的CSS定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù)
這篇文章主要介紹了Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù),本篇文章將介紹如何使用 Python 編寫一個簡單的網(wǎng)絡(luò)爬蟲,從網(wǎng)頁中提取有用的數(shù)據(jù),需要的朋友可以參考下2023-04-04
python實現(xiàn)數(shù)獨游戲 java簡單實現(xiàn)數(shù)獨游戲
這篇文章主要為大家詳細介紹了python實現(xiàn)數(shù)獨游戲和java實現(xiàn)數(shù)獨游戲的相關(guān)代碼,比較兩種語言實現(xiàn)數(shù)獨游戲的區(qū)別2018-03-03
Python采集天天基金數(shù)據(jù)掌握最新基金動向
這篇文章主要介紹了Python采集天天基金數(shù)據(jù)掌握最新基金動向,本次案例實現(xiàn)流程為發(fā)送請求、獲取數(shù)據(jù)、解析數(shù)據(jù)、多頁爬取、保存數(shù)據(jù),接下來來看看具體的操作過程吧2022-01-01
Python實現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法詳解
這篇文章主要為大家詳細介紹了Python實現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2023-02-02

