Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pickling error in python?

Tags:

python

pickle

I am getting this error, and I dont know what it means. How can I fix this problem?

my code looks like this, I've used it before and it has worked:

parentdir = os.getcwd()
dirlist = os.listdir(parentdir)

for dir in dirlist:
    if not dir == "pubs_edits": continue
    if os.path.isdir(os.path.join(parentdir, dir)):
                        os.chdir(os.path.join(parentdir, dir))
                        file_list = os.listdir(os.path.join(parentdir, dir))
                        for f in file_list:
                            in1 = open(f, 'r')
                            dict2 = pickle.load(in1)

This is the error message:

    File "/home/md202/pmid_editor.py", line 18, in <module>
        dict2 = pickle.load(in1)
    File "/usr/lib/python2.5/pickle.py", line 1370, in load
        return Unpickler(file).load()
    File "/usr/lib/python2.5/pickle.py", line 858, in load
        dispatch[key](self)
KeyError: '\x00'
like image 886
marsx Avatar asked Jun 13 '11 14:06

marsx


People also ask

What is the pickling in Python?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

What Cannot be pickled in Python?

With pickle protocol v1, you cannot pickle open file objects, network connections, or database connections.

Why pickle is not good in Python?

Pickle constructs arbitrary Python objects by invoking arbitrary functions, that's why it is not secure. However, this enables it to serialise almost any Python object that JSON and other serialising methods will not do. Unpickling an object usually requires no “boilerplates”.

What is pickling and Unpickling in Python with example?

The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshalling. We can converts the byte stream (generated through pickling) back into python objects by a process called as unpickling.


1 Answers

This exact error occurred for me when I tried to unpickle (using pickle.loads) a string representation that I had stored in a database via django. Django changed the charactee representation of my string so that pickle.loads(mystring) threw me that error. When I added an explicit string conversion in, it was fine: pickle.loads( str(mystring) )

EDIT: looking at the comments on the original post, I think this is related to the unicode string issue mentioned. I put a normal string into the database, and django gives me back a unicode string that produces this error.

like image 188
Matt Avatar answered Sep 21 '22 02:09

Matt