_pickle.UnpicklingError:找不到MARK [英] _pickle.UnpicklingError: could not find MARK

查看:2795
本文介绍了_pickle.UnpicklingError:找不到MARK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用成员为空集的成员参与者腌制EventFrame对象(的列表)时,出现类似 UnicodeDecodeError 的异常.

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)

这似乎不是一个好选择,因为引发了 UnpicklingError :

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.

注意,该错误是在加载pickle文件时引发的.

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

并进行转储:

f = open("myfile", "r+b")
pickle.dump(frame_list, f)
f.close()   

推荐答案

由于文件的偏移量不在开头,因此引发错误_pickle.UnpicklingError: could not find MARK.解决方法是在加载泡菜之前调用f.seek(0).

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.

这篇关于_pickle.UnpicklingError:找不到MARK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆