Convert String to Dictionary question

Brian Kelley bkelley at wi.mit.edu
Fri Feb 15 16:40:02 EST 2002


MDK wrote:


> Maybe cPickle won't work for me:
> 
>   File "C:\Python22\!MyTest\pipe.py", line 88, in put
>     pic = cPickle.dumps(object,1)
> cPickle.UnpickleableError: Cannot pickle <type 'array'> objects


Array's aren't inherently picklable.  You might have to do something 
like this.

text = pickle.dumps((object.typecode, object.tolist()))

and load with

object = apply(array.array, pickle.loads(text))

Of course this completely removes the reason to use pickle though since 
you need to know the datatype in the first place...

It also looks like your SOL when trying to subclass from array.array or 
array.ArrayType for including pickle cababilities.  The array module is 
really confusing in this respect, array.array is a function that returns 
an array.ArrayType which thinks of itself as <type 'array.array'> when 
it really is <type 'array.ArrayType'>.

Numeric arrays in contrast are picklable.

import Numeric
object = Numeric.array([1,2,3], Numeric.Float)
text = pickle.dumps(object)


Brian Kelley






More information about the Python-list mailing list