How do I unpickle a series of objects from a file

2019-07-13 21:03发布

I have pickled objects to a file in append mode but it only reads a single object. Here's the code. I don't know what I am doing wrong.

with open('notes.pkl', 'ab') as fileObject:                #append
    pickle.dump(obj, fileObject, pickle.HIGHEST_PROTOCOL)

with open('notes.pkl', 'rb') as input:                     #read
    obj= pickle.load(input)
    //perform tasks for each obj unpickled from the file

标签: python pickle
1条回答
Rolldiameter
2楼-- · 2019-07-13 21:38

You need to call pickle.load repeatedly until you hit end-of-file.

查看更多
登录 后发表回答