On 3 Oct 2006 16:58:17 -0700, MRAB <[EMAIL PROTECTED]> wrote: > > I like your idea Matthew but I don't know how to pickle the many > > variables in one file. Do I need to pickle each and every variable into > > a seperate file? > > var1,var2 > > pickle.dump(var1,f) > > pickle.dump(var2,f2) > > > Using the 'pickle' module: > > # To store: > f = open(file_path, "wb") > pickle.dump(var1, f) > pickle.dump(var2, f) > f.close() > > # To load > f = open(file_path, "rb") > var1 = pickle.load(f) > var2 = pickle.load(f) > f.close() > > A more flexible alternative is to use the 'shelve' module. This behaves > like a dict: > > # To store > s = shelve.open(file_path) > s["var1"] = "first" > s["var2"] = [2, 3] > s.close() > > # To load > s = shelve.open(file_path) > print s["var1"] # This prints "first" > print s["var2"] # This prints [2, 3] > s.close()
As long as we're on the subject of data serialization, I should like to bring up PyYaml. YAML is a portable format for storing data of all kinds; it became popular via Ruby I think, but there are implementations for many other languages. If you stick to storing simple stuff like lists, strings, and dictionaries, you can use your YAML data almost anywhere, but PyYaml even supports reifying things like lambdas. -- http://mail.python.org/mailman/listinfo/python-list