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)
       str_file.write(str1+'\n')

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:

 

Alabama,Montgomery
Alaska,Juneau
Arizona,Phoenix

and the resulting dictionary should be:

&lt;strong&gt;{'Alabama': 'Montgomery', 'Alaska' : 'Juneau', 'Arizona': 'Phoenix'} &lt;/strong&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:
    statecap= af.read()
    states = statecap.strip().split('\n')
    state_capitals={}
    for each in states:
        (stat,cap)=each.split(',')
        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 https://github.com/akshaypai/FlashCardQuiz

Advertisements

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

    1. 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.

      1. 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?

  1. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s