cPickle.load() question
Irmen de Jong
irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Sun Jun 8 14:10:15 EDT 2003
Dan Jones wrote:
> Irmen de Jong wrote:
>
>> Dan Jones wrote:
>>
>>> Hello all!
>>>
>>> I'm new to python and am trying to write a simple program to keep a
>>> list of my customers and generate pdf invoices for them (haven't
>>> gotten that far yet, any pdf module recommendations?). I have defined
>>> a customer class and a function to pickle them into a file. I'm just
>>> not sure how to go about reading the customer file back in. Is there
>>> a way to iterate over a pickled file and cPickle.load() each entry
>>> into a list?
>>
>>
>>
>> Just pickle the whole list instead of each customer object separately!
>> Then the unpickling will return your whole list of customer objects :-)
>>
>> --Irmen
>>
>>
> Thanks a lot, I didn't even think of doing it that way. :)
While pickling the list is certainly easier, I would still like to
comment that you can pickle.load() multiple times from a file object
if that file object contains multiple pickled objects.
If you've pickled X customers to a file, where the first pickled
object is an integer specifying the number of customers, you can do:
number = pickle.load(myFile)
for i in range(number):
customer = pickle.load(myFile)
....
-- Irmen
More information about the Python-list
mailing list