Python——shelve模块

shelve 模块

shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型。


case

1
2
3
4
5
6
7
8
9
10
import shelve

user = {'name':'ooc'}
f = shelve.open(r'user_info.shv',writeback = True)
f['user'] = user
f.close() # 关闭时,把添加的内容写回到原文件中

f = shelve.open(r'user_info.shv')
print(f['user'])
f.close()