[Tutor] pickle problems

eryksun eryksun at gmail.com
Sun Aug 12 03:58:56 CEST 2012


On Sat, Aug 11, 2012 at 9:18 PM, eryksun <eryksun at gmail.com> wrote:
>
> On line 68 you open the file in 'ab' mode. A pickle stream ends with
> an ASCII period (\x2e). Anything appended after that is ignored. Use
> 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary,
> so you might want a file extension other than .txt, such as .pkl,
> .dat, .bin, etc.

To clarify, you can store multiple pickles in a file, but each needs
its own load. So you'd have to maintain a session dictionary for the
factors of new integers. Then append the pickled session to the file
when the user quits. When the program starts you'd have to loop
through the file to update D with each pickled session.

If you want an approach that's simpler and faster, use the shelve
module. A shelf is a dictionary-like object that uses pickle to
serialize objects stored to a database. The keys have to be strings,
so your code would change to  `D[str(n)] = factors`.

http://docs.python.org/py3k/library/shelve.html#module-shelve


More information about the Tutor mailing list