[Tutor] cPickle usage

Kent Johnson kent37 at tds.net
Thu Aug 25 15:19:51 CEST 2005

Jorge Louis de Castro wrote:
> Hi,
> [Sorry for the repost, there was a typo previously]
> I think I may have misinterpreted the syntax of cPickle. I have dumped data 
> onto a file using:
> output = codecs.open(".\\"+self.filename, "ab")

Use plain open(), not codecs.open(), as you are not writing encoded character data. Why are you opening for append? Is the data in the file already? Are you skipping over it on the read? Otherwise use "wb" instead of "ab".

> cPickle.dump(self.terms, output)
> cPickle.dump(self.username, output)
> cPickle.dump(self.age, output)
> cPickle.dump(self.gender, output)
> cPickle.dump(self.totalMsgs, output)
> cPickle.dump(self.occurrences, output)
> I thought I could unpickle this using the load feature, something like:
> inFile = codecs.open(".\\"+self.filename, "r")

Use open() and mode "rb".
> cPickle.load(self.terms, inFile)

Shoud be 
self.username = cPickle.load(inFile) etc. I think you will get a TypeError here...


> cPickle.load(self.username, inFile)
> cPickle.load(self.age, inFile)
> cPickle.load(self.gender, inFile)
> cPickle.load(self.totalMsgs, inFile)
> cPickle.load(self.occurrences, inFile)
> Which would unpickle the data onto the variables. It does not work like 
> that, unfortunately. When I try reading the whole file I only get the first 
> object read as output.
> Any ideas how to achieve what this?
> chrs
> j.
>>Tutor maillist  -  Tutor at python.org
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list