I. Introduction in PYTHON3 we use JSON or pickle persisted data, can dump multiple times, but only load once, because the previous data has been overwritten by the data behind the dump. If we want to implement dump and load multiple times, we can use the shelve module. The shelve module can persist all data types supported by the pickle.
Code:
#持久化数据import Shelveimport datetime# info = {' name ': ' Bigberg ', ' age ': 22}# name = [' Apoll ', ' zous ', ' Luna ']# t = DateTime. DateTime.Now () # # with Shelve.open (' Shelve.txt ') as f:# f[' name '] = name # Persistent List # f[' info '] = info # Persistent Dictionary # f[' time ' = t # persistence-Type # # # # # # # # # # # # # # # # # # # # 3 Files Generated: Shelve.txt.bak, Shelve.txt.dat, Shelve.txt.dir. #删除 # with Shelve.open (' Shelve.txt ') as f:# del f[' name '] #获取数据: Use Getimport shelve# with Shelve.open (' Shelve.txt ') as F : # n = f.get (' name ') # i = f.get (' info ') # now = F.get (' time ') # # print (n) # print (i) # print (now) # output
Summarize:
1, shelve module is a simple key,value memory data through the file persistence module. 2. The shelve module can persist any Python data format that pickle can support. 3, shelve is a package of pickle module. 4, shelve module can be multiple dump and load.
Python shelve module