Question

I am trying to use pickling and I can't. It seems that I am doing something wrong. What is it? (using python 2.7)

In [2]: import cPickle as pickle

In [3]: arr = []

In [4]: tuple = ('name', 'surname', 'addr', 'area')

In [5]: arr.append(tuple)

In [6]: pickled_data=pickle.dumps(arr).encode("base64")

In [7]: pickled_data
Out[7]: 'KGxwMQooUyduYW1lJwpTJ3N1cm5hbWUnClMnYWRkcicKUydhcmVhJwp0cDIKYS4=\n'

In [8]: new_arr = pickle.loads(pickled_data).decode("base64")
---------------------------------------------------------------------------
UnpicklingError                           Traceback (most recent call last)
----> 1 new_arr = pickle.loads(pickled_data).decode("base64")

UnpicklingError: invalid load key, 'x'.
Was it helpful?

Solution

It should be:

new_arr = pickle.loads(pickled_data.decode("base64"))

First you decode base64, then unpickle it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top