import pickle
class Bird(object):
have_feather = True
way_of_reproduction = 'egg'
summer = Bird()
pickleString = pickle.dumps(summer) # serialize objectimport pickle
class Bird(object):
have_feather = True
way_of_reproduction = 'egg'
summer = Bird()
fileName = 'save.pkl'
with open(fileName, 'w') as f: # open file with write-mode
pickleString = pickle.dump(summer, f) # serialize and save object对象summer存储在文件save.pklimport pickle
# define the class before unpickle
class Bird(object):
have_feather = True
way_of_reproduction = 'egg'
fileName = 'save.pkl'
with open(fileName, 'r') as f:
summer = pickle.load(f) # read file and build objectimport cPickle as pickle就不须要再做不论什么修改了。
Python学习笔记12:标准库之对象序列化(pickle包,cPickle包)
原文:http://www.cnblogs.com/mengfanrong/p/4029611.html