一、什么叫序列化
??将原本的字典、列表等内容转换成一个字符串的过程就叫做序列化。
二、序列化的目的
1、以某种存储形式使自定义对象持久化;
2、将对象从一个地方传递到另一个地方。
3、使程序更具维护性。
三、
只有字符串能写入文件中
能再网络上传输的只能是bytes --- 字符串
把要传输的和要储存的内容 转换成 字符
字符串 转换回 要传输和储存的内容
四、序列化只有两种作用
???网络传输
???数据持久化 - 写在文件里
五、json模块
json 是各个语言通用的,数据类型
1.在网络上传输,比较频繁
?????ret = json.dumps(数据结构)
?????print(json.loads(ret))
2.dump,load用在文件的操作数据类型的序列化与反序列化上
????json.dump(数据结构,文件f)
????json.load(f)
??????????????dumps,dump(序列化)字典,列表 ?---------------------------》字符串 ???????????????loads,load(反序列化)
注意:元组相当于列表进行序列化,但一般不会用
例子1:dumps,loads例题d = {‘key1‘:‘values1‘,‘key2‘:‘values2‘}ret = json.dumps(d)print(d,type(ret)) ???????????????????????????#<class ‘str‘>print(json.loads(ret),type(json.loads(ret))) ?#<class ‘dict‘>例子2:dump,load例题 先序列化再写,只能操作一个字典with open("log","w",encoding="utf-8") as f1: ????json.dump(d,f1,ensure_ascii=False) ?#True 中文写进去是以gbk的方式写进去的with open("log","r",encoding="utf-8") as f1: ???for line in f1: ???????print(json.loads(line.strip()))例子3:ensure_ascii关键字参数data = {‘username‘:[‘李华‘,‘二愣子‘],‘sex‘:‘male‘,‘age‘:16}with open("log","w",encoding="utf-8") as f1: ???json.dump(data,f1,ensure_ascii=False)with open("log", "r", encoding="utf-8") as f1: ???print(json.load(f1))例子4:格式化输出data = {‘username‘:[‘李华‘,‘二愣子‘],‘sex‘:‘male‘,‘age‘:16}# (数据结构,倒叙,前面空格数量,","代替",")json_dic2 = json.dumps(data,sort_keys=True,indent=10,separators=(‘,‘,‘:‘),ensure_ascii=False)print(json_dic2)
六、pickle
用于序列化的两个模块json,用于字符串和python数据类型间进行转换
pickle,用于python特有的类型和python的数据类型间进行转换
pickle 模块提供了四个功能:dumps、dump(序列化,存)、
??????????????????????loads(反序列化,读)、load ?(不仅可以序列化字典,列表...可以把python中任意的数据类型序列化)
例题1:dumps,loadsdic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘}str_dic = pickle.dumps(dic)print(str_dic) ????#一串二进制内容dic2 = pickle.loads(str_dic)print(dic2) ??????#字典例题2:dump loadimport times_time = time.localtime(1000000000)print(s_time)with open("log","wb") as f1: ???s_time = pickle.dump(s_time,f1)with open("log","rb") as f1: ???s_time2 = pickle.load(f1) ???print(s_time2)例题3:dump loaddic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘}with open("log","wb") as f1: ???pickle.dump(dic,f1) ???pickle.dump(dic, f1)with open("log","rb") as f1: ???print(pickle.load(f1)) ???print(pickle.load(f1))
七、json 和 pickle ?的区别
1.pickle模块 dumps之后是bytes
2.pickle模块 dump之后的内容在文件中是乱的
3.pickle模块可以连续dump数据进入文件,然后连续load出来
4.pickle可以任意的将python中的数据类型序列化
???json只能对列表 字典 进行序列化
class A:pass ?# ?程序
a = A()
b = pickle.dumps(a)
print(b)
print(pickle.loads(b))
八、shelve
shelve也是python提供给我们的序列化工具,比pickle用起来更简单一些。
shelve只提供给我们一个open方法,是用key来访问的,使用起来和字典类似。
(存取存取)
import shelvewith shelve.open(‘shelve_file‘) as f1: ???f1[‘key‘] = {‘int‘:10, ‘float‘:9.5, ‘string‘:‘Sample data‘} ?#直接对文件句柄操作,就可以存入数据with shelve.open(‘shelve_file‘) as f1: ???existing = f1[‘key‘] ?????????????#取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报 ???print(existing)# key:{‘int‘:10, ‘float‘:9.5, ‘string‘:‘Sample data‘}with shelve.open(‘shelve_file‘,writeback=True) as f1: ???f1[‘key‘][‘new_value‘] = ‘this was not here before‘with shelve.open(‘shelve_file‘) as f1: ???print(f1[‘key‘])# {‘int‘: 10, ‘float‘: 9.5, ‘string‘: ‘Sample data‘, ‘new_value‘: ‘this was not here before‘}
json模块,pickle模块,shelve模块
原文地址:https://www.cnblogs.com/lara0520/p/8504578.html