Store and read data between dictionary and text file in python

Dictionary, a very god example of hash mapping data structure (hash table) where the  Contents are stored in <key,value> pair. The dictionaries are referred to, by their  keys and hence they have to be unique for a particular dictionary. where as the value might be same for multiple keys. To save the contents of the dictionary ,one of the available methods is to store it in a file.

There are other efficient methods to store python data structures like python pickle for example. Pickle helps in object serialization and efficient storage. Using pickle, you can store something by using a process called pickling and to read it back you need to un-pickle it using a python program. Though this is efficient, it cannot be used when you want to read the stored data without a python script. To put it simply, python pickle does not store data in human readable form.

I am working on a small project and it involves  generating a symbol and an address and storing them onto a dictionary. I spent quite a lot of time ,trying different techniques to store that data  into a text file  and this post explains the method that suited me the most.

In my case, i had a dictionary where each key had a single value.
for example: if the dictionary was-

dict1 = {'ZERO': '1000' , 'LOOP': '1003' , 'FWD': '1009'}

and this data should be stored in the file as :

ADD 1000
LOOP 1003
FWD 1009

So to achieve this, we have to iterate over the dictionary for each key and join the key and value separated by a space and stored in the file line wise.To do that the code is-

with open('storefile.txt','a') as str_file:
    for i in range(len(dict1)):
       temp = list(dict1.popitem())
       str1 = ' '.join(temp)

All we have done here is to create a  file named storefile.txt  in write mode to store the contents. then each item( both the key and value)  is extracted from the dictionary and concatenated  (having a space between) and it is written into the file. we are inserting a  ‘\n’  (newline character) while writing into file, so that that the data is written line wise. This code can be put inside a function or can be executed sequentially, we will get the same result.

Now, if we want to read the file and load the contents into the dictionary we can achieve this as described below:

Consider that we have file with state and capitals ,separated by a comma, that is:



and the resulting dictionary should be:

&amp;lt;strong&amp;gt;{'Alabama': 'Montgomery', 'Alaska' : 'Juneau', 'Arizona': 'Phoenix'} &amp;lt;/strong&amp;gt;

to do this let us consider the data to be in the file capitals.txt . Following code can be used to load the data.

with open('capitals.txt) as af:
    states = statecap.strip().split('\n')
    for each in states:
        state_capitals[stat] = cap

Here we use the with keyword to use the resource ,perform operations and closes the resource. it is a control flow structure. What we have done here is that we first read the contents of the file and extracted each line of the file as an item. Then, we store each of the line onto a list named states. 

The next step is to extract the list elements and separate the state and capitals stored separated by comma and store it separately into 2 variables  state and cap.  The final step is to put the state variable as key and cap variable as value into the dictionary state_capitals.

The retrieving of data into dictionary was for another quiz game that I had implemented. That game can be found on


akshay pai

I am a data science engineer and I love working on machine learning problems. I have experience in computer vision, OCR and NLP. I love writing and sharing my knowledge with others. This is why I created Source Dexter. Here I write about Python, Machine Learning, and Raspberry Pi the most. I also write about technology in general, books and topics related to science. I am also a freelance writer with over 3 years of writing high-quality, SEO optimized content for the web. I have written for startups, websites, and universities all across the globe. Get in Touch! We can discuss more.

4 thoughts on “Store and read data between dictionary and text file in python

  • Pingback:pickling python objects | ferretfarmer

    • September 18, 2013 at 4:25 pm

      yes, i know that the technique requires more optimization. But due to my requirements being basic , i used this to fullfill my needs. But thank you for the opinion.

      • November 7, 2013 at 11:38 pm

        I saw that you solved a less general problem, in a way that’s easier to read.

        It’s cool that you found a way to explain things clearly; reading the post was fun, and seeing your solution inspired me to try and ignore the ease of reading/explanation, and try to correctly solve the more general problem.

        I haven’t thought about optimizations yet – I tried to solve the problem more generally; maybe I’ve accidentally solved it more efficiently, too?

  • November 8, 2013 at 2:52 am

    yes, for sure pickling of the data is more efficient and the example you have given shows it all, but when the data is picked, if im not wrong its not in human readable form, right? so if anyone would like to store in a way its in human readable form , then have you found out any other way to store it?
    coz pickled data has to be unpickled to see the contents in that file. But i must agree that for a general case pickling is very efficient for storing data onto a disk.


Leave a Reply

%d bloggers like this: