首页 > 代码库 > Learn Python From 'Head First Python' [3](2) : Pickle

Learn Python From 'Head First Python' [3](2) : Pickle

1.the use of ‘with open... as ...‘

2.the use of pickle(dump and load)

for Step1:

the ‘with open ... as...‘ is the short format of ‘try...except...finally‘

 

for Step2:

you can store a list with pickle.dump() and get the content again with pickle.load()

 1 >>> import pickle 2 >>> with open(test.pickle,wb) as data: 3     pickle.dump([a,2,et],data) 4  5      6 >>> with open(test.pickle,rb) as readFile: 7     the_list = pickle.load(readFile) 8  9     10 >>> the_list11 [a, 2, et]12 >>> 

 

when use this method, you should be care with the encoding of file, like ‘wb‘,‘rb‘

Learn Python From 'Head First Python' [3](2) : Pickle