Python学习之路(十六):基础知识之序列化模块

一、序列化模块之json

序列:列表/元组/字符串/bytes

将其他的数据类型转换成字符串bytes 等序列化的过程。

为什么要学习序列化模块呢?数据类型之间的转换不是还另有方法吗?比如:

1 str_dic = str([1, 2, 3])
2 print(str_dic, type(str_dic))
3 res = eval(str_dic)
4 print(res, type(res))

但是这样,如果我们接受到的内容是恶意的话,强制转换后执行会带来一定的严重后果。

使用序列化模块可以将已知的代码根据自己的逻辑进行拼接,保存到文件中在适当的时候进行反序列化进而进行调用。

1.json.dumps()以及json.loads()

import json
1
dic = {'key': 'value', 'key2': 'value2'} 2 import json 3 ret = json.dumps(dic) # 序列化 4 print(dic, type(dic)) 5 print(ret, type(ret)) 6 7 res = json.loads(ret) # 反序列化 8 print(res, type(res))

使用这一对可以将序列化的内容存到文件中,并且通过反序列化调用

import json
1
dic = {1: 'value1', 2: 'value2'} 2 ret = json.dumps(dic) 3 with open('json_file', 'a') as f: 4 f.write(ret) 5 # 读取文件 6 with open('json_file', 'r') as f: 7 str_dic = f.read() 8 dic = json.loads(str_dic) 9 print(dic.keys())

2.json.dump()以及json.load()

 import json
1
# dump/load直接操纵文件 ,而dumps和loads操作内存 2 dic = {'key1': 'value1', 'key2': 'value2'} 3 with open('json_file', 'a') as f: 4 json.dump(dic, f) # 可多次dump 5 with open('json_file', 'r') 6 json.load(f) # 不可多次load,文件中只支持一个变量存在,如一个字典等 7 # 想把字典一个个放入文件中,再一个个取出来 8 dic = {'key1': 'value1', 'key2': 'value2'} 9 with open('json_file', 'a') as f: 10 str_dic = json.dumps(dic) 11 f.write(str_dic + '\n') 12 str_dic = json.dumps(dic) 13 f.write(str_dic + '\n') 14 str_dic = json.dumps(dic) 15 f.write(str_dic + '\n') 16 17 with open('json_file', 'r') as f: 18 for line in f: 19 dic = json.loads(line.strip()) 20 print(dic.keys())

3.编码

import json
1
dic = {'key': '你好'} 2 print(json.dumps(dic)) 3 # 结果:{"key": "\u4f60\u597d"} 4 5 # 如果不进行编码,原样输出 6 dic = {'key': '你好'} 7 print(json.dumps(dic, ensure_ascii=False)) 8 # 结果:{"key": "你好"}

4.使用json模块会出现的问题

1 # 会出现的问题1:数字经过转换变为字符串类型
import json
2 dic = {1: 'value1', 2: 'value2'} 3 ret = json.dumps(dic) 4 print(dic, type(dic)) 5 print(ret, type(ret)) 6 7 res = json.loads(ret) 8 print(res, type(res))
1 # 问题2.元组转换称为列表,且不可逆
import json
2 dic = {1: [1, 2, 3], 2: (1, 5, 'aa')} 3 ret = json.dumps(dic) 4 print(dic, type(dic)) 5 print(ret, type(ret)) 6 7 res = json.loads(ret) 8 print(res, type(res))
1 # 问题3.不可转换的模块set集合
import json
2 s = {1, 3, 'aaa'} 3 json.dumps(s)
1 问题4.字典中的key必须是字符串类型keys must be str, int, float, bool or None, not tupl
import json
2 json.dumps({(1, 3, 4): 133}) 3 4 son能处理的字符串类型非常有限:字符串、列表、字典、数字

二、序列化模块之pickle

1.pickle支持几乎所有的python数据类型

1 dic = {(1, 2, 3): {'a', 'b'}, 1: 'abc'}
2 ret = pickle.dumps(dic)
3 print(ret)
4 res = pickle.loads(ret)
5 print(res)

2.dumps序列化的结果只能是字节
3.只能在python中使用,而json支持其他程序类型如java等

4.dump/load在与文件交互时需要用wb,rb模式

1 dic = {(1, 2, 3): {'a', 'b'}, 1: 'abc'}
2 with open ('pickle_file', 'wb') as f:
3     pickle.dump(dic, f)
4 
5 with open('pickle_file', 'rb') as f:
6     ret = pickle.load(f)
7     print(ret, type(ret))

5.可以多次dump和load

 1 dic = {(1, 2, 3): {'a', 'b'}, 1: 'abc'}
 2 with open ('pickle_file', 'wb') as f:
 3     pickle.dump(dic, f)
 4     pickle.dump(dic, f)
 5     pickle.dump(dic, f)
 6     pickle.dump(dic, f)
 7 
 8 with open('pickle_file', 'rb') as f:
 9     ret = pickle.load(f)
10     print(ret, type(ret))
11     ret = pickle.load(f)
12     print(ret, type(ret))

猜你喜欢

转载自www.cnblogs.com/Studying-Du/p/12389194.html