Hi Guys,

I have a nested dictionary that i would like to be populated from a file at the start of my prog and put back into the file at the end of the prog.

Could anyone direct me to somewhere where i could get info on how to do this??
Or does anyone have any advice on how to go about it..


Hi StepIre,

You could use the pickle module, which serializes and saves data structures, effectively allowing you to skip the whole messy business of file parsing. Example:

import pickle

# If you want to store a dictionary in a file...
d = { "a":"aardvark", "b":"ball", "c":"centurion" }
f = open("somefile.dat", "w")
pickle.dump(d, f)

# If you want to load the stored dictionary...
f = open("somefile.dat", "r")
d = pickle.load(f)
print d["a"]     # prints 'aardvark'

Is this what you were looking for?

Hope this helps!

nice explanation

thanks for such a quick response..

That actually looks perfect!
Being a newbie i never heard of that module.

Thanks again.. problem solved!

This question has already been answered. Start a new discussion instead.