Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_pickle.UnpicklingError: could not find MARK

Tags:

python

pickle

I got exceptions like UnicodeDecodeError raised when pickling (a list of) objects of EventFrame with a member participants that was an empty set.

class EventFrame:
    """Frame for an event"""
    def __init__(self, id=0):
        ...
        self.participants = set()
        ...

When it wasn't empty, there were no problems, so I first set participants to something and then pickled it. But during runtime it may happen that participants is emptied again.

So I tried to manually delete the object in this case. After that I dumped it again using pickle.

if len(frame.participants) == 0:
    frame_list.remove(frame)

That doesn't seem to be a good choice, because this UnpicklingError was raised:

....
frame_list.append (pickle.load(f))
_pickle.UnpicklingError: could not find MARK

I don't know what it means and I couldn't find anything useful about it.

Note that this error is raised on loading the pickle file.

Here is the way I'm picklng and unpickling:

f = open("myfile", "r+b")
frame_list = []
while 1:
    try:
        frame_list.append (pickle.load(f))
        frame_list = sum(frame_list, [])
    except EOFError:
        break
f.close()

and dumping:

f = open("myfile", "r+b")
pickle.dump(frame_list, f)
f.close()   
like image 702
Sadik Avatar asked Mar 08 '16 22:03

Sadik


2 Answers

I got this error at first _pickle.UnpicklingError: could not find MARK, but that was because I was using the class name in the module name. Once I removed it, it worked like a charm!

like image 144
Sam Mourad Avatar answered Oct 21 '22 11:10

Sam Mourad


The error _pickle.UnpicklingError: could not find MARK is raised because the offset of the file is not in the beginning. The solution is to call f.seek(0) before loading the pickle.

like image 25
jasaarim Avatar answered Oct 21 '22 12:10

jasaarim