序列化和反序列化(serializing and deserializing)对象结构
import pickle
#pickling the list
Carlist=[‘Toyota’,’Honda’,’BMW’,’Audi’, ‘Suzuki’]
with open ("carlist.pkl","wb") as carpickle:
pickle.dump(Carlist,carpickle)
#序列化函数:pickle.dump(P1,P2):
#argument:
#P1:被序列化的参数
#P2:file object
#unpickling the list
with open("carlist.pkl","rb") as carpickle:
mycar=pickle.load(carpickle)
print(mycar)
#反序列化函数:pickle.dump(P)