1. shelve模块
是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式。
1 import shelve
2 import datetime
3 d = shelve.open('shelve_test')
4 info = {'age':22,"job":'teacher'}
5 name = ['xiaolaizi','xaioming']
6 d["name"] = name #持久化列表
7 d["info"] = info #持久化字典
8 d['date'] = datetime.datetime.now()
9 d.close()
1 import shelve 2 import datetime 3 d = shelve.open('shelve_test') 4 5 print(d.get("name")) 6 print(d.get("info")) 7 print(d.get("date"))
运行结果:
['xiaolaizi', 'xaioming']
{'age': 22, 'job': 'teacher'}
2018-09-04 21:11:42.296226