_pickle.UnpicklingError: could not find MARK

2019-04-24 14:55发布

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

标签: python pickle
1条回答
Evening l夕情丶
2楼-- · 2019-04-24 15:21

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.

查看更多
登录 后发表回答