Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pickling using protocol 2: Python3->2 data

I am trying to data a data array created in Python3.2, pickle it, and then open it in Python2.7. However, there is some part of the data that Python2.7 is objecting to, even though on a sample of the data it does fine, and I was wondering how to figure out what was going wrong.

So in Python3.2:

import pickle
with open('c:\\test.pickle', mode='wb') as f:
    pickle.dump(t_array, f, 2)

Then, when reading in Python2.7:

import pickle
f = open('c:\\test.pickle', mode='rb')
t_data = pickle.load(f)

The error is:

File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
File "C:\Python27\lib\pickle.py", line 1217, in load_build
    setstate(state)
TypeError: must be char, not unicode

The data is an array of dicts, nested at most two deep, e.g:

{'key3': '3', 'key2': 1.1, 'key1': 1, 'dict': {'dkey2': 2, 'dkey1': 1}}

What's (probably) going wrong here? Is there any easy way to see what in the original (large) dataset is causing the problem?

like image 489
cohoz Avatar asked Feb 25 '12 21:02

cohoz


People also ask

How do you use pickle in Python 3?

First, import pickle to use it, then we define an example dictionary, which is a Python object. Next, we open a file (note that we open to write bytes in Python 3+), then we use pickle. dump() to put the dict into opened file, then close. Use pickle.

What is pickling data in Python?

Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it's the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network.

How do you load data with pickles?

Python Pickle load You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Simple!


Video Answer


1 Answers

Pickle isn't as good as it might look like. Security vulnerabilities and many issues like these.

A much better idea is to create your own save format, for example using json.

like image 77
orlp Avatar answered Oct 16 '22 23:10

orlp