Storing and accessing key-value pairs using dictionaries
One of the most common tasks when automating or programming is the storage of key-value pairs. Think, for example, of a configuration file or a user profile. These are long text files containing key-value pairs. So, we need a data structure in Python to accommodate this kind of variable.
In this recipe, you will see how to store configuration information in a dictionary, manipulate the values of said configuration, and print the values of our configuration afterward.
Getting ready
Open your code editor and start by creating a file called dictionaries.py
and a file called config.txt
. Next, navigate your terminal to the same directory in which you just created the dictionaries.py
file.
How to do it…
In this recipe, we will read a configuration file from the hard drive, create a dictionary to store the configuration information we retrieved from the file, and allow the user to query and change configuration...