Pickle Problem

Chris Rebert clp2 at rebertia.com
Tue Mar 3 05:34:00 EST 2009


On Tue, Mar 3, 2009 at 1:52 AM, Fab86 <fabien.hall at gmail.com> wrote:
> Hello,
>
> I am new to using Python and am looking at exporting some of my code
> into a seperate document.
>
> The code I am using for the pickle is:
>
> file = open('testdoc.txt', 'w')
>
> pickle.dump(res1.total_results_available,file)
> pickle.dump(res2.total_results_available,file)
> pickle.dump(res3.total_results_available,file)
> file.close()
>
> res1.total_results_available and others are simply integers which are
> recalled from the Yahoo Search API and they print fine cmd or console
> but when trying to pickle them they are displayed like this:
>
> I14
> .I152000000
> .I86000
> .

That's the contents of testdoc.txt after your program has written data
to it using pickle. With pickle's default settings, the version of the
format it uses (specifically, the oldest version) looks like that
(integers in base-10 prefixed with 'I', entries separated by a newline
and a period). It's *NOT* intended NOR guaranteed to be human-readable
(indeed, with alternate settings it uses a binary format which is
/unintelligible/ to the causal viewer). For human-readable
serialization, use the `json` module (among other possibilities).

>
> But in console simply printing these attributes I get:
>
> 14
> 152000000
> 86000

That's normal. Pickle reads in the data from the file and deserializes
it back into the proper Python objects.

>
> Can anyone help?

There's no problem to be solved here, just some explaining in order to
deepen your understanding.

Cheers,
Chris

-- 
Shameless self-promotion:
http://blog.rebertia.com



More information about the Python-list mailing list