[Tutor] Editing Pickled .dat files

Liam Clarke ml.cyresse at gmail.com
Sat Mar 4 14:39:54 CET 2006

Hi Ryan,

You're trying to use your file, dictionary.dat like a dictionary data
structure in Python.
They don't work the same way.


pickle_file = open("dictionary.dat", "r")
dictionary = cPickle.load(pickle_file)

elif choice == "2":
         pickle_file = open("dictionary.dat", "a")
        sentence = raw_input("Enter the word youd like to add: ")
        if sentence not in dictionary:
             definition = raw_input("Whats the translated version: ")
              dictionary[sentence] = definition
             print "\n\t'",sentence,"'", "Has been added to the dictionary."
             print "\n\tThat term already exists!"

Once you're done, you just "repickle" the dictionary.

pickle_file = open("dictionary.dat", "w") #Opened in write mode
cPickle.dump(dictionary, pickle_file)


Liam Clarke

More information about the Tutor mailing list