The Art of Pickling: Binary vs Ascii difficulties

Andrew Dalke adalke at mindspring.com
Thu Oct 14 16:33:57 EDT 2004


Bix wrote:
> I'm leaving the example at the bottom.  There is a variable, fmt,
> within the test0 function which can be changed from -1
> (pickle.HIGHEST_PROTOCOL) to 0 (ascii).  The behavior between the two
> pickle formats is not consistent.  I'm hoping for an explaination and
> a possible solution; I'd like to store my data in binary.

What's the inconsistancy?

Ahh, I see the comments down at the end of your file.  I
assume you think they should all pass?

They all pass for me.

I'll guess you're on MS Windows.  You need to open the file
in binary mode instead  of ascii, which is the default.
Try changing

  	pickle.Pickler(open(fn,'w'),fmt).dump(x)
  	obj = pickle.Unpickler(open(fn,'r')).load()

to

  	pickle.Pickler(open(fn,'wb'),fmt).dump(x)
  	obj = pickle.Unpickler(open(fn,'rb')).load()


This isn't clear in the documentation, as Skip complained
about last year in the thread starting at
   http://mail.python.org/pipermail/python-dev/2003-February/033362.html

Though to be precise, this isn't actually a pickle
issue.

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list