shelve模块
shelve是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式,是pickle更上一层的封装
1. 存储数据
1 import shelve,datetime 2 dd = shelve.open('shelve_test') 3 info = {"age":25,'job':'it'} 4 name = ["zhangsan","lisi","wangwu","zhaoliu"] 5 dd["name"] = name 6 dd["info"] = info 7 dd["date"] = datetime.datetime.now() 8 dd.close()
2. 取回数据
1 import shelve,datetime 2 d = shelve.open('shelve_test') 3 print(d.get("name")) 4 print(d.get("info")) 5 print(d.get("date"))
得到的结果如下:
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
{'age': 25, 'job': 'it'}
2017-08-17 09:47:43.473363
博主小学未毕业,请勿口喷!