[Tutor] Editing Pickled .dat files
ryan_gm at sbcglobal.net
Sat Mar 4 14:51:17 CET 2006
Ohhh ok i got it to work! but why isn't the change
permanent? it is "writing" to the file isn't it.
--- Liam Clarke <ml.cyresse at gmail.com> wrote:
> 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
> pickle_file = open("dictionary.dat", "w") #Opened in
> write mode
> cPickle.dump(dictionary, pickle_file)
> Liam Clarke
More information about the Tutor