[Tutor] (no subject)

John Fouhy john at fouhy.net
Fri Aug 26 00:09:50 CEST 2005


On 8/26/05, Jorge Louis de Castro <jobauk at hotmail.com> wrote:
> I think I may have misinterpreted the syntax of cPickle. I have dumped data
> onto a file using:
> 
> output = codecs.open(".\\"+self.filename, "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")
> cPickle.load(self.terms, inFile)
> cPickle.dump(self.username, inFile)
> cPickle.dump(self.age, inFile)
> cPickle.dump(self.gender, inFile)
> cPickle.dump(self.totalMsgs, inFile)
> cPickle.dump(self.occurrences, inFile)

Did you notice you have only one call to load() here? The others are
still dump().

>>> import pickle
>>> f = file('foo', 'ab')
>>> pickle.dump('foo', f)
>>> pickle.dump('bar', f)
>>> pickle.dump('baz', f)
>>> f.close()
>>> f = file('foo', 'rb')
>>> a = pickle.load(f)
>>> b = pickle.load(f)
>>> c = pickle.load(f)
>>> a,b,c
('foo', 'bar', 'baz')

Although, normally when I use pickle, I chuck everything into a tuple,
so I can store/retrieve it all with a single dump/load command..

-- 
John.


More information about the Tutor mailing list