Retrieving And Updating Data Contained in Shelve in Python
In Python shelve you access the keys randomly. In order to access the keys randomly in python shelve we use open() function. This function works a lot like the file open() function in File handling. Syntax for open the file using Python shelve
shelve.open(filename, flag='c' , writeback=True)
In Order to access the keys randomly in shelve in Python, we have to take three steps:
- Storing Python shelve data
- Retrieving Python shelve data
- Updating Python shelve data
Storing Python shelve data :
In order to store python shelve data, we have to create a file with full of datasets and open them with a open() function this function open a file which we have created.
# At first, we have to import the 'Shelve' module. import shelve # In this step, we create a shelf file. shfile = shelve.open("shelf_file") # we create a data object which in this case is a book_list. my_book_list =['bared_to_you', 'The_fault_in_our_stars', 'The_boy_who_never_let_her_go'] # we are assigning a dictionary key to the list # which we will want to retrieve shfile['book_list']= my_book_list # now, we simply close the shelf file. shfile.close() |
Retrieving Python shelve data :
After storing a shelve data, we have to retrieve some data from a file in order to do that we use index operator [] as we do in lists and in many other data types.
# At first, we import the 'Shelve' module. import shelve # In this step, we create a shelf file. var = shelve.open("shelf_file") # Now, this 'var' variable points to all the # data objects in the file 'shelf_file'. print(var['book_list']) # now, we simply close the file 'shelf_file'. var.close() |
Output :
['bared_to_you', 'The_fault_in_our_stars', 'The_boy_who_never_let_her_go']
Note : Output will be depend on what you have store in a file
Updating Python shelve data :
In order to update a python shelve data, we use append() function or we can easily update as we do in lists and in other data types. In order to make our changes permanent we use sync() function.
# At first, we have to import the 'Shelve' module. import shelve # In this step, we create a shelf file. var = shelve.open("shelf_file", writeback = True) # inputting total values we want to add # to the already existing list in shelf_file. val1 = int(input("Enter the number of values ")) for x in range(val1): val = input("\n Enter the value\t") var['book_list'].append(val) # Now, this 'var' variable will help in printing # the data objects in the file 'shelf_file'. print(var['book_list']) # to make our changes permanent, we use # synchronize function. var.sync() # now, we simply close the file 'shelf_file'. var.close() |
Input :
Enter the number of values 5 Enter the value Who moved my cheese? Enter the value Our impossible love Enter the value Bourne Identity Enter the value Hush Enter the value Knock-Knock
Output :
['bared_to_you', 'The_fault_in_our_stars', 'The_boy_who_never_let_her_go', 'Who moved my cheese?', 'Our impossible love', 'Bourne Identity', 'Hush', 'Knock-Knock']
Note : Input and Output depend upon user, user can update anything in a file which user want according to user input, output will be changed.
Recommended Posts:
- Python | setting and retrieving values of Tkinter variable
- Python | Updating value list in dictionary
- Python | Check if a list is contained in another list
- Python for Data Science
- Python | Data visualization using Bokeh
- Inbuilt Data Structures in Python
- Python | Data analysis using Pandas
- Exploratory Data Analysis in Python
- Data analysis and Visualization with Python
- Data Analysis and Visualization with Python | Set 2
- Multidimensional data analysis in Python
- Exploratory Data Analysis in Python | Set 1
- Working With JSON Data in Python
- SQL using Python | Set 3 (Handling large data)
- Python | Pandas Series.data
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



