Pickle EOFError saving lists

那年仲夏 提交于 2020-04-16 06:18:08

问题


I am trying to save multidimensional lists into a file, while keeping the structure of the lists. I figured using pickle would be the easiest:

for ... stuff:
    # stuff happens which creates the new list kWebsTemp that I want to save
    # now for every loop-run I want to append my file by adding the new list as an additional object

    with open('%s_%s' %(filename,Scent), 'wb') as file:
                        #file.write(kWebsTemp)
                        #file.write('\n')

                        pickle.dump(kWebsTemp, file) 

If I now however try to load the lists, I am only able to load the first list that was saved.

with open('%s_%s' %('testData','E'), 'rb') as file:
        print pickle.load(file)
        print pickle.load(file)

So for the second print command I get the EOFError. Any idea whats going wrong here?


回答1:


'wb' has to be replaced for 'ab' in order to actually append the file for an object.



来源:https://stackoverflow.com/questions/47391799/pickle-eoferror-saving-lists

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