序列化

初识序列化与反序列化

  • 对象信息数据结构信息通过转换达到存储或者传输效果
  • 可以用比特的编码与解码进行联想

可序列化的数据类型

  • number
  • str
  • list
  • tuple
  • dict # 最常用的

Python中的json模块

方法名 参数 介绍 举例 返回值
dumps obj 对象序列化 json.dumps([1,2]) 字符串
loads str 返序列化 Json.loads('[1,2,3]') 原始数据类型

Python中的pickle

方法名 参数 介绍 举例 返回值
dumps obj 对象序列化 pickle.dumps([1,2]) 比特
loads byte 返序列化 pickle.loads('[1, 2, 3]') 原始数据类型

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# coding:utf-8

import json


def read(path):
with open(path, 'r') as f:
data = f.read()

return json.loads(data)


def write(path, data):
with open(path, 'w') as f:
if isinstance(data, dict):
_data = json.dumps(data)
f.write(_data)
else:
raise TypeError('data is dict')
return True


data = {'name': '小慕', 'age': 18, 'top': 176}

if __name__ == '__main__':
# write('test.json', data)
result = read('test.json')
result['sex'] = 'boy'
write('test.json', result)
# print(result, type(result))