pickle
- 序列化(持久化,落地):把程序运行中的信息保存在磁盘上
- 反序列化: 序列化的逆过程
pickle
: Python 提供的序列化模块
pickle.dump
: 序列化
pickle.load
: 反序列化
举例
>>> import pickle # 下面的例子就不重复这个导入操作了
>>>
“腌制泡菜”
>>> age = 18
>>> with open("text.txt", "wb") as f:
... pickle.dump(age, f)
>>>
# text.txt
€K.
“取出泡菜”
>>> f = open("text.txt", "rb") # 这里用二进制是必要的
>>> f.readline()
b'x80x03Kx12.'
>>> f.seek(0)
0
>>> pickle.load(f)
18
>>>
序列化
>>> lst = [18, 19, 20, "york", "fish", [2, 3]]
>>> with open("text.txt", "wb") as f:
... pickle.dump(lst, f)
...
>>>
反序列化
>>> with open("text.txt", "rb") as f:
... pickle.load(f)
...
[18, 19, 20, 'york', 'fish', [2, 3]]
>>>
注意事项
pickle
几乎可以把所有 Python 的对象都转换为二进制的形式存放
- 用
pickle
序列化,需要以二进制打开
- 若要将
pickle
后的内容保存为文件,推荐以 .pkl
或 .pickle
为后缀
shelve
- 持久化工具
- 存取方式类似字典,用
k
, v
保存数据
>>> import shelve # 下面的例子就不重复这个导入操作了
>>>
举例
>>> shv = shelve.open("shv.db")
>>> shv["one"] = 1
>>> shv["two"] = 2
>>> shv["three"] = 3
>>> shv.close()
>>>
- Windows 下,上述操作过后,相应文件夹内出现三个文件
shv.db.bak
shv.db.dat
shv.db.dir
>>> shv = shelve.open("shv.db")
>>> try:
... print(shv["one"])
... print(shv["four"])
... except KeyError as e:
... print(e)
... finally:
... shv.close()
...
1
b'four'
>>>
特性
- 不支持多个应用并行写入
- 写回问题
shelv
默认情况下,不会接受持久化对象进行任何修改
- 解决方法:强制写回
writeback=True
>>> shv = shelve.open("shv.db", flag='r')
>>> try:
... k = shv["one"]
... print(k)
... finally:
... shv.close()
...
1
>>>
>>> shv = shelve.open("shv.db")
>>> try:
... shv["one"] = {'a':65, 'b':98, 'c':99}
... finally:
... shv.close()
...
>>> shv = shelve.open("shv.db")
>>> try:
... one = shv["one"]
... print(one)
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>>
强制写回
问题
shelve
关闭,内容仍存于内存中,没有写回数据库
>>> shv = shelve.open("shv.db")
>>> try:
... k = shv["one"]
... print(k)
... k['a'] = 97
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>>
解法
>>> shv = shelve.open("shv.db", writeback=True)
>>> try:
... k = shv["one"]
... print(k)
... k['a'] = 97
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>> shv = shelve.open("shv.db")
>>> try:
... print(shv["one"])
... finally:
... shv.close()
...
{'a': 97, 'b': 98, 'c': 99}
>>>
使用 with 管理上下文环境
>>> with shelve.open("shv.db", writeback=True) as shv:
... print(shv["one"])
... shv["one"]['a'] = 0
...
{'a': 97, 'b': 98, 'c': 99}
>>> with shelve.open("shv.db") as shv:
... print(shv["one"])
...
{'a': 0, 'b': 98, 'c': 99}
>>>