Saving and loading multiple objects in pickle file?
Two additions to Tim Peters' accepted answer.
First, you need not store the number of items you pickled separately if you stop loading when you hit the end of the file:
def loadall(filename):
with open(filename, "rb") as f:
while True:
try:
yield pickle.load(f)
except EOFError:
break
items = loadall(myfilename)
This assumes the file contains only pickles; if there's anything else in there, the generator will try to treat whatever else is in there as pickles too, which could be dangerous.
Second, this way, you do not get a list but rather a generator.
This will load only one item into memory at a time, which is useful
if the dumped data is very large -- one possible reason why you may
have wanted to pickle multiple items separately in the first place.
You can still iterate over items
with a for
loop as if it were
a list.
Try this:
import pickle
file = open('test.pkl','wb')
obj_1 = ['test_1', {'ability', 'mobility'}]
obj_2 = ['test_2', {'ability', 'mobility'}]
obj_3 = ['test_3', {'ability', 'mobility'}]
pickle.dump(obj_1, file)
pickle.dump(obj_2, file)
pickle.dump(obj_3, file)
file.close()
file = open('test.pkl', 'rb')
obj_1 = pickle.load(file)
obj_2 = pickle.load(file)
obj_3 = pickle.load(file)
print(obj_1)
print(obj_2)
print(obj_3)
file.close()
Using a list, tuple, or dict is by far the most common way to do this:
import pickle
PIK = "pickle.dat"
data = ["A", "b", "C", "d"]
with open(PIK, "wb") as f:
pickle.dump(data, f)
with open(PIK, "rb") as f:
print pickle.load(f)
That prints:
['A', 'b', 'C', 'd']
However, a pickle file can contain any number of pickles. Here's code producing the same output. But note that it's harder to write and to understand:
with open(PIK, "wb") as f:
pickle.dump(len(data), f)
for value in data:
pickle.dump(value, f)
data2 = []
with open(PIK, "rb") as f:
for _ in range(pickle.load(f)):
data2.append(pickle.load(f))
print data2
If you do this, you're responsible for knowing how many pickles are in the file you write out. The code above does that by pickling the number of list objects first.