[Tutor] pickle.dump yielding awkward output

Alan Gauld alan.gauld at btinternet.com
Mon Feb 4 02:03:45 CET 2013

On 03/02/13 19:26, Spyros Charonis wrote:
> I am experiencing a strange result with the pickle module when using it
> to write certain results to a separate file.

The only strangec results using pickle would be if the uinpickle failed 
to bring back that which was pickled.
Pickle is a storage format not a display format.

> In short, I have a program that reads a file, finds lines which satisfy
> some criteria, and extracts those lines, storing them in a list.

Extracting them with pickle I hope? That's the only thing that should be 
used to unpickle a pickled file.

> The list of extracted lines looks like this:
> ATOM      1  N   GLN A   1      29.872  13.384  54.754  1.00 60.40
>      N
> The output stored from the call to the pickle.dump method, however,
> looks like this:
> (lp0
> S'ATOM      1  N   GLN A   1      29.872  13.384  54.754  1.00 60.40
>        N  \r\n'

Yep, I'm sure pickle can make sense of it.

> Does anyone know why the strings lp0, S', aS' are showing up?

Because that's what pickle puts in there to help it unpickle it later.

Why do you care? You shouldn't be looking at it (unless you want to 
understand how pickle works).

pickle, as the name suggests, is intended for storing python objects
for later use. This is often called object persistence in programming 
parlance. It is not designed for anything else.

If you want cleanly formatted data in a file that you can read in a text 
editor or similar you need to do the formatting yourself or use another 
recognised format such as CSV or configparser (aka ini file).

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list