0

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

Thanks!!

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by StepIre
1

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)
f.close()

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

Is this what you were looking for?

Hope this helps!

Votes + Comments
nice explanation
0

Hey,
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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.