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()
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 callf.seek(0)
before loading the pickle.