python中zip和unzip數(shù)據(jù)的方法
本文實(shí)例講述了python zip和unzip數(shù)據(jù)的方法。分享給大家供大家參考。具體實(shí)現(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)"
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python3 selenium自動(dòng)化測(cè)試 強(qiáng)大的CSS定位方法
今天小編就為大家分享一篇python3 selenium自動(dòng)化測(cè)試 強(qiáng)大的CSS定位方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
基于Python實(shí)現(xiàn)本地音樂播放器的制作
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)本地音樂播放器的制作,并且可以選擇需要播放的音樂的路徑,選擇播放方式,感興趣的小伙伴可以了解一下2022-06-06
Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁(yè)數(shù)據(jù)
這篇文章主要介紹了Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁(yè)數(shù)據(jù),本篇文章將介紹如何使用 Python 編寫一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲,從網(wǎng)頁(yè)中提取有用的數(shù)據(jù),需要的朋友可以參考下2023-04-04
python實(shí)現(xiàn)數(shù)獨(dú)游戲 java簡(jiǎn)單實(shí)現(xiàn)數(shù)獨(dú)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)數(shù)獨(dú)游戲和java實(shí)現(xiàn)數(shù)獨(dú)游戲的相關(guān)代碼,比較兩種語(yǔ)言實(shí)現(xiàn)數(shù)獨(dú)游戲的區(qū)別2018-03-03
Python采集天天基金數(shù)據(jù)掌握最新基金動(dòng)向
這篇文章主要介紹了Python采集天天基金數(shù)據(jù)掌握最新基金動(dòng)向,本次案例實(shí)現(xiàn)流程為發(fā)送請(qǐng)求、獲取數(shù)據(jù)、解析數(shù)據(jù)、多頁(yè)爬取、保存數(shù)據(jù),接下來來看看具體的操作過程吧2022-01-01
淺析對(duì)torch.unsqueeze()函數(shù)理解
torch.unsqueeze()函數(shù)起到升維的作用,dim等于幾表示在第幾維度加一,這篇文章主要介紹了對(duì)torch.unsqueeze()函數(shù)理解深度解析,感興趣的朋友跟隨小編一起看看吧2024-06-06
Python實(shí)現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-02-02
python 發(fā)送和接收ActiveMQ消息的實(shí)例
今天小編就為大家分享一篇python 發(fā)送和接收ActiveMQ消息的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
解決Python發(fā)送Http請(qǐng)求時(shí),中文亂碼的問題
這篇文章主要介紹了解決Python發(fā)送Http請(qǐng)求時(shí),中文亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

