I'm trying to get to grips with using files, particurlarly saving a set of inputs from the user into a file and then load it all back up again.
I have a program which, through a little object oriented interactivity, allows the user to pair up names and numbers in a dictionary. But now I want to be able to save it in file, preferably in form like this:

number;name

I've read a few tutorials and about the cpickle method, but I'm simply lost on how to do something like this in real life. Anyone know of some good - thourough - reading material or even have the time to write an explanation?

pickling is more beneficial for custom classes. For a built-in class (dictionary, list, etc.) simply use eval (NOTE: this solution has been posted before on this forum, but I couldn't find it)

>>> d1 = {'a':2, 'b':3}
>>> d2 = eval(repr(d1))
>>> d2
{'a': 2, 'b': 3}
>>> d2['c']=1
>>> d1
{'a': 2, 'b': 3}
>>> d2
{'a': 2, 'c': 1, 'b': 3}

So for what you're looking for you'd write the repr() bit to a file, and then use eval after reading said file.

*I think the previous solution used execfile instead of bothering to open said file; however like I said I couldn't find it and don't remember exactly, but it answered exactly what you're asking.

Otherwise, use the forum search and look for "pickle" to find examples of dumping/loading pickled objects.

But now I want to be able to save it in file, preferably in form like this:

number;name

It's a simple combination so a common text file will work just fine. There is also the advantage of being able to verify what you have written to the file via any text processor. Note that dictionaries hash keys so they are not necessarily in key order.

def read_from_file():
   after_d = {}
   fp = open("test.txt", "r")
   for rec in fp:
      rec=rec.strip()
      key, name = rec.split(";")
      after_d[int(key)]=name        ## convert key back to an integer
   fp.close()
   return after_d


def save_to_file(save_d):
   print "before saving to file", save_d
   fp = open("test.txt", "w")
   for key in save_d.keys():
      fp.write("%d;%s\n" % (key, save_d[key]))  ## writes everything as a string
   fp.close()
   

before_d = {}
before_d[1]="Name 1"
before_d[2]="Name 2"
before_d[3]="Name 3"
before_d[10]="Name 10"
before_d[20]="Name 20"

save_to_file(before_d)
after = read_from_file()
print "read from file       ", after

Thanks, this helps a lot. I'm ever so slowly learning to actually like working with python, and every little stupid question answered is a step in that direction.

You really should use module pickle for that ...

# use module pickle to save/dump and load a dictionary object
# or just about any other intact object

import pickle

# create the test dictionary
before_d = {}
before_d[1]="Name 1"
before_d[2]="Name 2"
before_d[3]="Name 3"

# pickle dump the dictionary
fout = open("dict1.dat", "w")
pickle.dump(before_d, fout)
fout.close()

# pickle load the dictionary
fin = open("dict1.dat", "r")
after_d = pickle.load(fin)
fin.close()

print before_d  # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
print after_d   # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
This question has already been answered. Start a new discussion instead.