'_pickle.UnpicklingError: could not find MARK

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()   


Solution 1:[1]

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.

Solution 2:[2]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jasaarim
Solution 2 Sam Mourad