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")

shelve序列化

import shelve,datetime

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块

可以持久化任何pickle可支持的python数据格式

#a=shelve.open("new_year")

name=["alex","jack","tomo"]
info={"age":22,"job":"it","name":"alex"}
date=datetime.datetime.now()

a["name"]=name
a["indo"]=info
a["date"]=date

a.close()

读取文件内容

import shelve,datetime
a=shelve.open("new_year")
print(a.get("indo"))