The Art of Pickling: Binary vs Ascii difficulties

Josiah Carlson jcarlson at uci.edu
Thu Oct 14 16:18:23 EDT 2004


> 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.

If you want to store data in binary, and are running on windows, you
must make sure to open all files with the binary flag, 'b'.

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

The above should be open(fn, 'wb')... and open(fn, 'rb')... respectively.

Changing those two made all of them pass for me, and I would expect no
less.

Oh, and so that you get into the habit early; tabs are frowned upon as
indentation in Python.  The standard is 4 spaces, no tabs.

 - Josiah




More information about the Python-list mailing list