#!/usr/bin/env python
#import json#不同语言交互,只能存简单的字符,集合等,不能存函数
'''
info = {
'name':'lian',
'age':22,
}
'''
'''
f = open("test.test",'w')
f.write(str(info))
f.close()
'''
'''
f = open("test.test",'r')
data = eval(f.read())
f.close()
print(data['age'])
'''
'''
f = open("test.test",'w')
print(json.dumps(info))
f.write(json.dumps(info))
f.close()
'''
'''
f = open("test.test",'r')
data = json.loads(f.read())
print(data['age'])
'''
import pickle#功能与json一样,能功能更强,能存json不能存的函数等,但函数在反序列化中也要再定义
def sayhi(name):
print('hello',name)
info = {
'name':'lian',
'age':22,
'func':sayhi
}
'''
f = open("test.test",'wb')
print(pickle.dumps(info))
f.write(pickle.dumps(info))
f.close()
'''
'''
f = open("test.test",'rb')
data = pickle.loads(f.read())
print(data['age'])
print(data['func']('lian'))
'''
'''
f = open("test.test",'wb')
pickle.dump(info,f)#等同于f.write(pickle.dumps(info))
f.close()
'''
'''
f = open("test.test",'rb')
data = pickle.load(f)#等同于pickle.loads(f.read())
print(data['age'])
print(data['func']('lian'))
f.close()
'''