python3 shelve模块

f = shelve.open(filename)    # 通过open方法打开一个文件,并拿到文件对象f
f['a'] = "Python's obj"        # 以字典的方式将Python中的任意对象持久化

a = f['a']
a = 'new str'
f['a'] = a

f.close()       # 最后要显示的调用close方法,保存并关闭f对象
# 如果觉得每次f.close()麻烦,请用with负责管理:
with shelve.open(filename) as f:
    f['auth'] = {'name': 'wang', 'age': 18}
shelve可以用来持久化任意的Python的对象,pickle能处理的,shelve都能持久化,一般我们通过字典的形式来操作shelve对象
shelve对象支持字典支持的所有方法。
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10996796.html