Easiest way to persist a data structure to a file in python? -


let's have this:

d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] } 

what's easiest way progammatically file can load python later?

can somehow save python source (from within python script, not manually!), import later?

or should use json or something?

use pickle module.

import pickle d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] } afile = open(r'c:\d.pkl', 'wb') pickle.dump(d, afile) afile.close()  #reload object file file2 = open(r'c:\d.pkl', 'rb') new_d = pickle.load(file2) file2.close()  #print dictionary object loaded file print new_d 

Comments