_pickle.UnpicklingError: could not find MARK

I have exceptions, such as UnicodeDecodeError , raised when sorting (listing) EventFrame objects with participant members that were empty.

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

When it was not empty, there were no problems, so I first put the participants on something, and then marinated. But at run time, it may happen that the participants are empty again.

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

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

This does not seem to be a good choice, because this UnpicklingError has been raised:

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

I do not know what this means, and I could not find anything useful.

Note that this error occurs when loading a pickle file.

This is how I write and crumble:

 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 reset:

 f = open("myfile", "r+b") pickle.dump(frame_list, f) f.close() 
+5
source share
1 answer

The _pickle.UnpicklingError: could not find MARK error occurs because the file offset is not at the beginning. The solution is to call f.seek(0) before loading the brine.

+4
source

Source: https://habr.com/ru/post/1244690/


All Articles