_pickle.UnpicklingError: could not find MARK

大憨熊 提交于 2019-12-10 02:21:06

问题


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

回答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.



来源:https://stackoverflow.com/questions/35879096/pickle-unpicklingerror-could-not-find-mark

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!