import pickle
def sh(name):
print("hello,",name)
#序列化存储
info={‘key‘:‘mode‘,
‘lis‘:‘koud‘,
‘olo‘:234,
‘func‘:sh
}
pickle可复杂序列化
f=open("text.txt","wb")
print(pickle.dumps(info)) #pickle序列化以二进制存储
f.write(pickle.dumps(info))
pickle.dump(info,f) #优化后的写法效果完全相同
f.close()
pickle反序列化
import pickle #只有在python语言里用
def sh(name): #序列化整个函数
print("hello3,",name)
print("hello4,",name)
f=open("text.txt","rb") #pickle反序列化读取的是二进制数据
data=pickle.loads(f.read())
data=pickle.load(f) #优化写法效果完全相同
print(data"func")
原文地址:http://blog.51cto.com/12992048/2176886
时间: 2024-11-07 11:35:38