3.4 .序列化模块
-
序列化模块(非常非常重要 )
- 序列化:将一个数据结构 (list,dict....)转化成一个特殊的序列(特殊的字符串)的过程.
- 反序列化:将这个特殊的序列反转回原来的数据结构。
l1 = [1,2,3] ret = str(l1) print(ret,type(ret)) print(eval(ret))#不用 文件存取时,遇到的矛盾. dic = {'username': '太白', 'password': 123} dic = {1: {'username': '太白', 'password': 123,'status': False}, 2:{'username': 'alex', 'password': 123,'status': False} } with open('register.json',encoding = 'utf-8',mode='w+')as f: f1.write(str(dic)) f.seek(0) print(ret,type(ret)) 数据结构 --- >str()存储在文件 读取出来时 反转不回去(报错)
-
网络传输
l1 = [i for i in range(10)] 凡是数据通过网络传出去最终的格式必须bytes s1 = str(l1) b1 = s1.encode('utf-8') print(b1) #b'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]' b1可以发送出去 s2 =b1.decode('utf-8') print(s2,type(s2))#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'str'> s2 转化不成列表了.
我们现在要解决的问题: 如果有一种特殊的字符串,这个字符串可以与任何的数据结构互相转换.
-
序列化模块: 将一中数据结构转化成特殊的序列(特殊的字符串,bytes)并且还可以反转回去.
json模块: 是所有语言公认的一种序列.最最常用的 所以支持的python数据结构有限: int str bool dict list(tuple),None,float None ---> Null ----> NUll dict ---> pickle模块: 只能python语言中使用的,序列化模块: 支持python所有的数据类型以及对象. shevle模块(不讲): 课下了解(只能是文件存取).
-
json序列化
两种四个方法.
dumps,loads
import json dumps,loads 主要用于网络传输,可以用于文件的存取. dic = {'username': '太白', 'password': 123,'status': False} ret = json.dumps(dic) print(ret,type(ret))#{"username": "\u592a\u767d", "password": 123, "status": false} <class 'str'> ret_dict = json.loads(ret) print(ret_dict)#{'username': '太白', 'password': 123, 'status': False}<class 'dict'>
dumps,loads 写入文件
特殊的参数 dic = {'username': '太白', 'password': 123,'status': False} ret = json.dumps(dic,ensure_ascii=False,sort_keys=True) print(ret,type(ret))#{"password": 123, "status": false, "username": "太白"} <class 'str'> 以json写入文件,dumps转化成特殊的字符串 s_dict = json.dumps(dic) with open('jsonlx.json',encoding='utf-8',mode='w') as f1: f1.write(s_dict) with open('jsonlx.json',encoding='utf-8') as f2: content = f2.read() print(json.loads(content))
dump load:单个数据的存取文件.
dic = {'username': '太白', 'password': 123,'status': False} with open('jsonlx1.json',encoding='utf-8',mode='w') as f1: json.dump(dic,f1) with open('jsonlx1.json',encoding='utf-8') as f1: dic1 = json.load(f1) print(dic1,type(dic1))
-
多个数据如何存储到一个文件中?
# 错误演示: # import json # dic1 = {'username': '太白', 'password': 123,'status': False} # dic2 = {'username': 'alex', 'password': 123,'status': False} # dic3 = {'username': 'ly', 'password': 123,'status': False} # with open('jsonmore.json',encoding='utf-8',mode='a') as f1: # # f1.write(json.dumps(dic1)) # # f1.write(json.dumps(dic2)) # # f1.write(json.dumps(dic3)) # f1.write(f'{json.dumps(dic1)}{json.dumps(dic2)}{json.dumps(dic3)}') # with open('jsonmore.json',encoding='utf-8') as f1: # ret = json.loads(f1.read()) # print(ret)
正确做法: dic1 = {'username': '太白', 'password': 123,'status': False} dic2 = {'username': 'alex', 'password': 123,'status': False} dic3 = {'username': 'ly', 'password': 123,'status': False} with open('jsonmore.json',encoding='utf-8',mode='a') as f1: f1.write(f'{json.dumps(dic1)}\n{json.dumps(dic2)}\n{json.dumps(dic3)}') with open('jsonmore.json',encoding='utf-8') as f1: for line in f1: ret = json.loads(line) print(ret,type(ret))
-
pickle:pickle模块是将Python所有的数据结构以及对象等转化成bytes类型,然后还可以反序列化还原回去。
dumps,loads 只能是网络传输
import pickle l1 = ['wusir', '太白', '小黑', 666] ret = pickle.dumps(l1) print(ret)#b'\x80\x03]q\x00(X\x05\x00\x00\x00wusirq\x01X\x06\x00\x00\x00\xe5\xa4\xaa\xe7\x99\xbdq\x02X\x06\x00\x00\x00\xe5\xb0\x8f\xe9\xbb\x91q\x03M\x9a\x02e.' l2 = pickle.loads(ret) print(l2,type(l2))#['wusir', '太白', '小黑', 666] <class 'list'>
dump load 数据结构存取文件.
写入单个文件
import pickle l1 = ['wusir', '太白', '小黑', 666] with open('pickle练习.pickle',mode='wb') as f1: pickle.dump(l1,f1) with open('pickle练习.pickle', mode='rb') as f1: ret = pickle.load(f1) print(ret,type(ret))
多个数据写入文件
l1 = ['wusir', '太白', '小黑1', 666] l2 = ['wusir', '太白', '小黑2', 666] l3 = ['wusir', '太白', '小黑3', 666] with open('pickle练习1.pickle',mode='wb') as f1: pickle.dump(l1,f1) pickle.dump(l2,f1) pickle.dump(l3,f1) with open('pickle练习1.pickle', mode='rb') as f1: ret1 = pickle.load(f1) ret2 = pickle.load(f1) ret3 = pickle.load(f1) print(ret1,ret2,ret3) #['wusir', '太白', '小黑1', 666] ['wusir', '太白', '小黑2', 666] ['wusir', '太白', '小黑3', 666]