'''
json:
用于不同程序之间的数据交换
dumps loads:json格式处理函数
dump load:主要用来读写json文件函数
pickle:
主要用来读写json文件函数,读取和存储都以字节形式。函数和json的差不多
shelve:
shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写
'''
file_path = 'json.txt'
dn = {'baidu': 'www.baidu.com', 'qq': 'www.qq.com', '360': 'www.360.cn'}
name = ['mayun', 'mahuateng', 'liyanhong']
import json
def json_dump():
with open(file_path,'w') as f_write,\
open(file_path,'r') as f_read:
json.dump(dn,f_write)
f_write.close()
data = json.load(f_read)
print(data)
def json_dumps():
with open(file_path,'w') as f_write,\
open(file_path,'r') as f_read:
f_write.write(json.dumps(dn))
f_write.close()
data = f_read.read()
dicts = json.loads(data)
print(dicts)
import pickle
def pickle_dump():
with open(file_path,'wb') as f_write,\
open(file_path,'rb') as f_read:
pickle.dump(dn,f_write) ##写一个对象
pickle.dump(name,f_write) ##再写一个对象
f_write.close()
data=pickle.load(f_read)
print(data)
data = pickle.load(f_read)
print(data)
import shelve
def shelve_test():
f = shelve.open(file_path)
f['baidu'] = 'www.baidu.com'
f['qq'] = 'www.qq.com'
f['360'] = 'www.360.cn'
f.close()
f = shelve.open(file_path)
print(dict(f))
if __name__ == '__main__':
# json_dump()
# json_dumps()
# pickle_dump()
shelve_test()