python模块详解 shelve

shelve模块是一个简单的k,v 将内存数据通过文件持久化的模块,可以持久化任何pickle可以支持的python数据。简单的说对 pickle的更上一层的封装。

写文件

import shelve
d  = shelve.open('test4') #这个文件不用存在,运行自动生成
name = ['hello','china','!']
d["test"] = name #k,v
d.close()

 运行成功后会在当前目录下多3个文件。分别是test4.bak,test4.dat,test4.dir。

读文件

print(d.get('test')) #['hello', 'china', '!']
原文地址:https://www.cnblogs.com/qing-chen/p/7286934.html