把内存数据转成字符,叫序列化,dump,dumps
把字符转成内存数据类型,叫反序列化load,loads
dumps:仅转成字符串
dump不仅能把对象转换成str,还能直接存到文件内
json.dumps json.loads
1.把你的内存数据,通过网络,共享给远程其他人
2.定义了不同语言之间的交互规则
a.纯文本,缺点:不能共享复杂的数据类型,比如字典
b.xml ,缺点:占空间大
c.json,优点,简单,可读性好
pickle:
dumps 转换为bytes,写到硬盘后是pickle自己的格式,不可直接读,看着像是乱码,其实不是
json:
支持的数据类型:str,int,tuple,list,dict
跨平台,跨语言,体积小
pickle:
支持python所有的数据类型
只能在python中使用,存储数据占空间大
shelve 对pickle进行封装,可进行多次dump,load,只能在python中使用,以k_v结构写入文档,每次dump都要写K
1 import shelve 2 f = shelve.open("shelve_test") 3 names = ["alex","rain","test"] 4 info = {"names":"alex","age":22} 5 f["names"] = names 6 f["info_dic"] = info 7 f.close() 8 #获取数据 9 # f = shelve.open("shelve_test") 10 # print(list(f.keys()))#[‘names‘, ‘info_dic‘] 11 # print(list(f.items()))#[(‘names‘, [‘alex‘, ‘rain‘, ‘test‘]), (‘info_dic‘, {‘names‘: ‘alex‘, ‘age‘: 22})] 12 # print(f.get("names"))#[‘alex‘, ‘rain‘, ‘test‘] 13 # print(f.get("info_dic"))#{‘names‘: ‘alex‘, ‘age‘: 22} 14 #修改数据,重新给K复制 15 # f = shelve.open("shelve_test") 16 # f["names"] = ["Alex","Rain","Test"] 17 # f.close() 18 # f = shelve.open("shelve_test") 19 # print(f.get("names"))#[‘Alex‘, ‘Rain‘, ‘Test‘] 20 #删除数据 21 # f = shelve.open("shelve_test") 22 # del f["info_dic"] 23 # f.close() 24 # f = shelve.open("shelve_test") 25 # print(f.get("info_dic"))#None 26 27 #添加数据 28 # f = shelve.open("shelve_test") 29 # f["scores"] = [1,2,3,4,5] 30 # f.close() 31 # f = shelve.open("shelve_test") 32 # print(f.get("scores"))#[1, 2, 3, 4, 5]