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

Recommended Answers

All 2 Replies

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!

commented: nice explanation +7

Hey,
thanks for such a quick response..

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

Thanks again.. problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.