[BangPypers] Pickle multiple objects

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Wed Oct 7 10:37:16 CEST 2009

I'm guessing you want to do something like this

>>> fo = file("test.pkl", "w")
>>> pickle.dump([1,2,3,4], fo)
>>> pickle.dump([5,6,7,8], fo)
>>> fo.close()
>>> fi = file("test.pkl")
>>> pickle.load(fi)
[1, 2, 3, 4]
>>> pickle.load(fi)
[5, 6, 7, 8]
>>> pickle.load(fi)

Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
line 1370, in load
return Unpickler(file).load()
line 858, in load
line 880, in load_eof
raise EOFError

You will need to handle the EOFError in a try catch block.


def pickledobjects(f):
while True:
yield pickle.load(f)
except EOFError:

objs = list(pickledobjects(file("fi")))

Though, is this isn't a large amount of data, I'd recommend just storing
the data in a single object and writing that to the file.

On Wed, Oct 7, 2009 at 1:14 PM, Aneesh A <aneesh.nl at gmail.com> wrote:

> Hi friends,
>    I am new to python world. I am doing a small python game ( command line
> based). The problem is:
> I have to store high scores, so i pickled a list . after pickling, in
> append mode, load method loads only first object.
> How to retrieve multiple objects??
> I am attaching a source.
> Look the alpha.

I am but a man.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20091007/f1fdd80f/attachment.htm>

More information about the BangPypers mailing list