今天就跟大家聊聊有關(guān)使用pickle怎么存儲(chǔ)dump和oad數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)專注于達(dá)拉特企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站定制開發(fā)。達(dá)拉特網(wǎng)站建設(shè)公司,為達(dá)拉特等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)使用pickle模塊來dump你的數(shù)據(jù)
import os import sys import pickle man=[ ] other=[ ] try: data=open('sketch.txt') for each_line in data: try: (role,line_spoken)=each_line.split(':',1) line_spoken=line_spoken.strip() if role == 'Man': man.append(line_spoken) elif role == 'Other Man': other.append(line_spoken) except ValueError: pass data.close() except IOError: nester.print_lol('The data file is missing!') try: with open('man_data.txt','wb') as man_file: pickle.dump(man,man_file) with open('other_data.txt','wb') as other_file: pickle.dump(other,other_file) except IOError as err: print('File error: ' + str(err)) except pickle.PickleError as perr: print('Pickling error: ' + str(perr))
打開man_data.txt,看結(jié)果:
?]q (X' Is this the right room for an argument?qX No you haven't!qX When?qX No you didn't!qX You didn't!qX You did not!qX= Ah! (taking out his wallet and paying) Just the five minutes.qX You most certainly did not!qX Oh no you didn't!q X Oh no you didn't!q X Oh look, this isn't an argument!qX No it isn't!qX It's just contradiction!q X It IS!qX You just contradicted me!qX You DID!qX You did just then!qX" (exasperated) Oh, this is futile!!qX Yes it is!qe.
把已存儲(chǔ)在man_data.txt上的二進(jìn)制文件,恢復(fù)成可以讀的文件,存放在new_man.txt中:
import nester import os import sys import pickle man=[ ] other=[ ] new_man=[ ] try: data=open('sketch.txt') for each_line in data: try: (role,line_spoken)=each_line.split(':',1) line_spoken=line_spoken.strip() if role == 'Man': man.append(line_spoken) elif role == 'Other Man': other.append(line_spoken) except ValueError: pass data.close() except IOError: print_lol('The data file is missing!') try: # with open('man_data.txt','wb') as man_file: # pickle.dump(man,man_file) # with open('other_data.txt','wb') as other_file: # pickle.dump(other,other_file) with open('man_data.txt','rb') as man_file: new_man=pickle.load(man_file) except IOError as err: print('File error: ' + str(err)) except pickle.PickleError as perr: print('Pickling error: ' + str(perr))
查看結(jié)果:
RESTART: C:/Users/ThinkPad/AppData/Local/Programs/Python/Python36-32/chapter4-134-pickle.py >>> import nester >>> nester.print_lol(new_man) Is this the right room for an argument? No you haven't! When? No you didn't! You didn't! You did not! Ah! (taking out his wallet and paying) Just the five minutes. You most certainly did not! Oh no you didn't! Oh no you didn't! Oh look, this isn't an argument! No it isn't! It's just contradiction! It IS! You just contradicted me! You DID! You did just then! (exasperated) Oh, this is futile!! Yes it is! >>> import os >>> os.getcwd() 'C:\\Users\\ThinkPad\\AppData\\Local\\Programs\\Python\\Python36-32' >>>
若是想保存new_man.txt到磁盤文件,可以加:
with open('new_man.txt','w') as new_man_file: nester.print_lol(new_man,fn=new_man_file)
看完上述內(nèi)容,你們對(duì)使用pickle怎么存儲(chǔ)dump和oad數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。