问题
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