[Tutor] Re: Fw: Please submit to tutor list: dictionary update
prob
Kent Johnson
kent37 at tds.net
Thu Jan 20 12:06:17 CET 2005
Eri Mendz wrote:
> Kent Johnson <kent37 <at> tds.net> writes:
>
>
>>Jacob S. wrote:
>>
>>>>sorry to send this to you but if you may, kindly send to tutor list as im
>>>>no longer subscribed. my problem is in the update dict portion: it just
>>>>doesnt update regardless how many contacts i add. kindly advise where
>>>>my mistake is or code gone wrong. the rest of the options i will do on my
>>>>own so just hold off the helps for now. appreciate all your good help.
>>
>>>>def update_dict(d, f):
>>>> ''' update the saved dictionary file '''
>>>>
>>>> read = open(f, 'rb')
>>>> newdic = cPickle.load(read)
>>>> newdic.update(d)
>>>> read.close()
>>
>>You don't do anything with newdic. My guess is you want to dump it back to the
>
> file so it is saved.
>
>
> this is what i tried:
> read = open(f, 'rb')
> newdic = cPickle.load(read)
> newdic.update(d)
> read.close()
>
> write = open(f, 'wb')
> cPickle.dump(f, write)
You still aren't doing anything with newdic. The absence of 'newdic' in the code after
'read.close()' should be a clue :-)
Check the docs on pickle.dump() (which is the same as cPickle.dump()):
dump( obj, file[, protocol[, bin]])
Write a pickled representation of obj to the open file object file. This is equivalent to
Pickler(file, protocol, bin).dump(obj).
So to pickle newdic you should say
cPickle.dump(newdic, write)
> write.close()
>
> but it 'overwrites' the saved dic. can you rw in one go, dump and load the
> updated dict instantly??
I think you want to overwrite the saved dict, but with the new dict instead of with a filename string...
Kent
>
> sending this in gmane mail2news gateway.
Thank you
>
More information about the Tutor
mailing list