Python Shelve Module - Python Cheatsheet
Save variables
>>> import shelve
>>> wife = ['Pretty', 'Lovely', 'Nice']
>>> with shelve.open('mydata') as shelf_file:
... shelf_file['wife'] = wife
Open and read variables
>>> with shelve.open('mydata') as shelf_file:
... print(type(shelf_file))
... print(shelf_file['wife'])
...
# <class 'shelve.DbfilenameShelf'>
# ['Pretty', 'Lovely', 'Nice']
Just like dictionaries, shelf
values have keys()
and values()
methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the list()
function to get them in list form.
>>> with shelve.open('mydata') as shelf_file:
... print(list(shelf_file.keys()))
... print(list(shelf_file.values()))
...
# ['wife']
# [['Pretty', 'Lovely', 'Nice']]