shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;
key必须为字符串,而值可以是python所支持的数据类型
用的比较少
目的: 将字典写入文件保存起来
import shelve f = shelve.open(r'shelve.txt') # f 就是一个字典 # f['stu1_info'] = {'name':'a', 'age':18, 'hobby':['pinao','smoking','drinking']} # f['stu2_info'] = {'name':'b', 'age':20, 'hobby':['pinao','smoking','drinking']} print(f['stu1_info']['hobby']) # ['pinao', 'smoking', 'drinking'] f.close()