使用pickle存儲數(shù)據(jù)dump 和 load實例講解
使用pickle模塊來dump你的數(shù)據(jù):對上篇博客里的sketch.txt文件:
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,看結果:
€]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.
把已存儲在man_data.txt上的二進制文件,恢復成可以讀的文件,存放在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))
查看結果:
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)
以上這篇使用pickle存儲數(shù)據(jù)dump 和 load實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python的數(shù)據(jù)類型與標識符和判斷語句詳解
在本篇文章里小編給大家整理了一篇關于python數(shù)據(jù)類型與標識符和判斷語句的介紹,有需要的朋友們可以學習下,希望能夠給你帶來幫助2021-09-09
pycharm出現(xiàn)了pytest模式下如何改回run模式
這篇文章主要介紹了pycharm出現(xiàn)了pytest模式下如何改回run模式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Python編程pygame模塊實現(xiàn)移動的小車示例代碼
這篇文章主要介紹了Python編程pygame模塊實現(xiàn)移動的小車示例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python函數(shù)的默認參數(shù)請勿定義可變類型詳解
這篇文章主要介紹了python函數(shù)的默認參數(shù)請勿定義可變類型詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
python獲得文件創(chuàng)建時間和修改時間的方法
這篇文章主要介紹了python獲得文件創(chuàng)建時間和修改時間的方法,涉及Python針對文件屬性的相關操作技巧,需要的朋友可以參考下2015-06-06

