shelve模块可用来存储字典文件,用法与字典类似
写数据:
1 import shelve 2 import datetime 3 4 d=shelve.open('shelve_test') 5 6 info={ 'age':23,'job':'IT'} 7 8 name=['Tao','Abc','Zi'] 9 10 d['name']=name #持久序列化11 d['info']=info12 d['date']=datetime.datetime.now()13 14 d.close()
运行结果:生成了三个文件
读数据:
1 import shelve2 3 d=shelve.open('shelve_test')4 print(d.get('name'))5 print(d.get('info'))6 print(d.get('date'))
运行结果: