0

I was wondering if there is any way to collect all the Object dumps from pickle file at once.

For example,

If I have a list say a = [1, 2, 3, 4, 5] and a dictionary say b = {1: "one", 2: "Two"} & I have dumped it into a file called database.pkl then while loading the data from the pickle file, I don't want to use multiple pickle.load call neither I want to use loops to do this.

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
  • You can't. See [this answer](http://stackoverflow.com/a/12762056/478656) - "*Pickle streams are entirely self-contained, and so unpickling will unpickle one object at a time. Therefore, to unpickle multiple streams, you should repeatedly unpickle the file*". The reasonable approach is @user2682863's answer to only put one container object in the pickle file, with everything else in it. Or use a loop - what's wrong with loops? – TessellatingHeckler Nov 21 '15 at 06:19
  • @TessellatingHeckler In loops you need to keep separate track of the length sometimes. For some applications where we are transferring the the information on a socket, I need to send the object number first & then the pickle file..If there would have been a separate method to get something like `*args` then we could have used it easily. However @user2682863's answer seems legitimate. – Mayukh Sarkar Nov 21 '15 at 06:29

1 Answers1

1

add everything you want to save/load to a dictionary

dic = {"a" : [1,2,3,4,5], "b" : .....}

then save/load that dictionary

user2682863
  • 3,097
  • 1
  • 24
  • 38