How do I unpickle a series of objects from a file in Python?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 00:07:37

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/36504887/how-do-i-unpickle-a-series-of-objects-from-a-file-in-python

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