import pickle
1、定义一个Person类
class Person:
def init(self,n,a):
self.name=n
self.age=a
def show(self):
print(self.name+”_”+str(self.age))
2、定义一个dataList
dataList = [[1, 1, ‘yes’],
[1, 1, ‘yes’],
[1, 0, ‘no’],
[0, 1, ‘no’],
[0, 1, ‘no’]]
3、定义一个字典
dataDic = { 0: [1, 2, 3, 4],
1: (‘a’, ‘b’),
2: {‘c’:’yes’,’d’:’no’}}
4、定义一个Person并赋值
aa = Person(“JGood”, 2)
aa.show()
5、使用dump()将数据序列化到文件中
f=open(‘d:\p.txt’,’wb’)
pickle.dump(aa,f,0)
pickle.dump(dataList,f,0)
pickle.dump(dataDic,f,0)
f.close()
6、打开文件,输出文件第1次装入的内容
del Person
f=open(‘d:\p.txt’,’rb’)
bb1=pickle.load(f) <