看例子:
import json
import pickle
def sayhi(name):
print('hello,',name)
info = {
'name':'Tim',
'age':22
}
info1 = {
'name':'Tim',
'age':22,
'func':sayhi
}
# 序列化,将内容写进内存
# f = open('test.text','w')
# f.write(json.dumps(info)) #json只能序列化简单的数据类型,如info,info1中包含一个函数,序列化会出错
# f.close()
# f = open('test1.text','wb')
# f.write(pickle.dumps(info1)) #pickle可以序列化任何数据类型,只用于python
# f.close()
# 反序列化,从内存读取内容
# f = open('test.text','r')
# data = json.loads(f.read())
# print(data)
f = open('test1.text','rb')
data = pickle.loads(f.read())
print(data)
网友评论