[Tutor] Editing Pickled .dat files
ryan luna
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.
>
> So.
>
> pickle_file = open("dictionary.dat", "r")
> dictionary = cPickle.load(pickle_file)
> pickle_file.close()
>
> 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."
> else:
> 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)
> pickle_file.close()
>
> Regards,
>
> Liam Clarke
>
More information about the Tutor
mailing list