Python 基础之序列化模块

痛定思痛。 2022-05-16 01:09 276阅读 0赞

序列化

概念

将原本的字典、列表等内容转换成一个字符串的过程就叫做序列化。如:将 Python 代码转为文本,方便移植,转化文本这个过程为序列化。

目的

  1. 以某种存储形式使自定义对象持久化;
  2. 转移对象,方便携带移植;
  3. 使程序更具有维护性。

70

json

使用 json 函数要先导入 json 函数库:import json

dump和dumps 序列化方法。

  • dump:必须传文件描述符,将序列化的文件保存在文件中。
  • dumps:把数据结构直接转化为 json 字符串形式(较为常用)。

将下列数据编码转化为json格式:

  1. import json
  2. data = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
  3. json = json.dumps(data)
  4. print(json, type(json))

结果:

{“a”: 1, “b”: 2, “c”: 3, “d”: 4, “e”: 5}

Process finished with exit code 0

可以看到输出的 json 为字符串对象,下面我们来对比一下 Python 中的数据结构转化 json 的形式为:






































Python json
dict object
list, tuple array
str, unicode string
int, long, float number
True true
False false
None null

load和loads 反序列化方法

  • load:用于接收文件描述符,完成读取文件和反序列化。
  • loads:把 json 字符串形式直接转化为数据结构(反序列化,较为常用)。

    import json

    json_data = ‘{“a”: 1, “b”: 2, “c”: 3, “d”: 4, “e”: 5}’
    content = json.loads(json_data)
    print(content, type(content))

结果:

{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}

Process finished with exit code 0

注意:字典引号,json 对象中字典的是双引号,否则报错。

反过来,我们来看看 json 类型转化为 Python 类型的对照表:










































json Python
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None

json与demjson

demjson 是 python 的第三方模块库,可用于编码和解码 json 数据,包含了 jsonLint 的格式化及校验功能。

Github 地址:https://github.com/dmeranda/demjson

官方地址:http://deron.meranda.us/python/demjson/

安装介绍:http://deron.meranda.us/python/demjson/install

encode和decode函数

  • encode:将 Python 对象编码成 json 字符串。
  • decode:将已编码的 json 字符串解码为 Python 对象

(实现代码如json,在这里不做演示。)

pickle

与 json 都是序列化模块,区别如下:

  • json:用于字符串 和 python 数据类型间进行转换。
  • pickle:用于 python 特有的类型 和 python 的数据类型间进行转换,pickle类型的数据为二进制。

pickle 中的方法和 json 相同,都有 load、loads、dump 和 dumps。下面演示 dumps 和 loads 用法:

  1. import pickle
  2. dict_data_one = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
  3. pickle_data = pickle.dumps(dict_data_one) # serialization
  4. print(pickle_data) # binary
  5. dict_data_two = pickle.loads(pickle_data) # deserialization
  6. print(dict_data_two)

结果:

b’\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02X\x01\x00\x00\x00cq\x03K\x03X\x01\x00\x00\x00dq\x04K\x04X\x01\x00\x00\x00eq\x05K\x05u.’
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}

Process finished with exit code 0

下面展示 dump 和 load 用法:

  1. import time
  2. import pickle
  3. structure_time_one = time.localtime()
  4. print(structure_time_one)
  5. f = open('pickle_file', 'wb')
  6. pickle.dump(structure_time_one, f)
  7. f.close()
  8. f = open('pickle_file', 'rb')
  9. structure_time_two = pickle.load(f)
  10. print(structure_time_two.tm_year,
  11. structure_time_two.tm_mon,
  12. structure_time_two.tm_mday)

结果:

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=13, tm_hour=21, tm_min=41, tm_sec=50, tm_wday=0, tm_yday=225, tm_isdst=0)
2018 8 13

Process finished with exit code 0

shelve

shelve 也是 python 提供给我们的序列化工具,比 pickle 用起来更简单一些。shelve 只提供给我们一个 open 方法,是用 key 来访问的,使用起来和字典类似。

  1. import shelve
  2. f = shelve.open('shelve_file')
  3. '''operate on the file handle and store the data'''
  4. f['key'] = {'int': 10, 'float': 9.5, 'string': 'Sample data'}
  5. f.close()
  6. f = shelve.open('shelve_file')
  7. content = f['key']
  8. f.close()
  9. print(content)

结果:

{‘int’: 10, ‘float’: 9.5, ‘string’: ‘Sample data’}

Process finished with exit code 0

这个模块有个限制,它不支持多个应用同一时间往同一个 DB 进行写操作。所以当我们知道我们的应用如果只进行读操作,我们可以让shelve 通过只读方式打开 DB。

由于 shelve 在默认情况下是不会记录待持久化对象的任何修改的,所以我们在 shelve.open() 时候需要修改默认参数,否则对象的修改不会保存。

  1. import shelve
  2. f1 = shelve.open('shelve_file')
  3. print(f1['key'])
  4. f1['key']['new_value'] = 'this was not here before'
  5. f1.close()
  6. f2 = shelve.open('shelve_file', writeback=True)
  7. print(f2['key'])
  8. f2['key']['new_value'] = 'this was not here before'
  9. f2.close()

writeback方式有优点也有缺点。优点是减少了我们出错的概率,并且让对象的持久化对用户更加的透明了;但这种方式并不是所有的情况下都需要,首先,使用 writeback 以后,shelf 在 open() 的时候会增加额外的内存消耗,并且当 DB 在 close() 的时候会将缓存中的每一个对象都写入到 DB,这也会带来额外的等待时间。因为 shelve 没有办法知道缓存中哪些对象修改了,哪些对象没有修改,因此所有的对象都会被写入。

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 python基础模块

    1.模块的简介 模块化指将一个完整的程序分解成一个个的小模块 通过将模块组合,来搭建出一个完整的程序 模块化的优点: 方便开发

    相关 python基础模块序列

    \---什么是序列化(picking)?  我们把变量从内存中变成可存储或传输的过程称之为序列化。  序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器

    相关 python基础模块序列

    \---什么是序列化(picking)?  我们把变量从内存中变成可存储或传输的过程称之为序列化。  序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器