python序列化、反序列化(pickle)

序列化:

import pickle
def sayhi(name):
    print("hello,",name)
info = {
    'name':'alex',
    'age':22,
    'func':sayhi
}
f = open("test.text","wb")
pickle.dump(info,f) #等于f.write( pickle.dumps( info) )
f.close()

反序列化:

import pickle
def sayhi(name):
    print("hello2,",name)
f = open("test.text","rb")
data = pickle.load(f) #等于data = pickle.loads(f.read())
print(data["func"]("sooele"))

 

发表评论