[Tutor] Delete in .dat
Bob Gailer
bgailer at alum.rpi.edu
Mon Mar 6 00:01:50 CET 2006
ryan luna wrote:
> My question seems like itd be farly simple but i cant
> seem to get it to work,
> My .dat file is a dictionary and i need to be able to
> only delete a single word and its definition,
> heres what i have so far but it doesn't work.
>
AARHG! I am always frustrated by "it doesn't work" This gives us
insufficient information. Please always present us with the specific
diagnostic.
"it doesn't work"
... Could mean del raises an exception..
... Could mean that the key is still in the dictionary after the del.
... Could mean that the key is still in the dictionary after you pickle
dump then open load.
... Could mean you see "...Has been removed"
... Could mean you see "...That word is not in the dictionary""
More information, please.
> pickle_file = open("dictionary.dat", "r")
>
> dictionary = cPickle.load(pickle_file)
> pickle_file.close()
> sentence = raw_input("What definition would
> you like to delete?: ")
> if sentence in dictionary:
> del dictionary[sentence]
> print "\n\t", sentence, "Has been removed"
> else:
> print "\n\t", sentence, "That word is not
> in the dictionary"
> pickle_file = open("dictionary.dat", "w")
> cPickle.dump(dictionary, pickle_file)
> pickle_file.close()
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
More information about the Tutor
mailing list