标签:python脚本 python文件管理 python 对象序列化 对象储存
计算机的内存中存储的是二进制的序列。import pickle class Bird(object): have_feather = True way_of_reproduction = 'egg' summer = Bird() pickleString = pickle.dumps(summer) # serialize object
import 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.pkl
import 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 object
import cPickle as pickle就不需要再做任何改动了。
标签:python脚本 python文件管理 python 对象序列化 对象储存
原文地址:http://blog.csdn.net/xufeng0991/article/details/40105893