标签:
文件读写涉及到cPickle这个包。基本的流程在代码中有表述,但是教程中说加一段代码叫做:protocol=cPickle.HIGHEST_PROTOCOL会使得效率大增
#!/usr/bin/env python # coding=utf-8 import cPickle f=file(‘obj.txt‘,‘wb‘) my_obj=3 cPickle.dump(my_obj,f,protocol=cPickle.HIGHEST_PROTOCOL) f.close() f=file(‘obj.txt‘,‘rb‘) load_obj=cPickle.load(f) f.close() print load_obj obj1=1 obj2=2 obj3=3 f=file(‘obj.txt‘,‘wb‘) for obj in [obj1,obj2,obj3]: cPickle.dump(obj, f,protocol=cPickle.HIGHEST_PROTOCOL) f.close() f=file(‘obj.txt‘,‘rb‘) obj=[] for i in range(3): obj.append(cPickle.load(f)) f.close() print obj
标签:
原文地址:http://www.cnblogs.com/taokongcn/p/4237387.html