How to save an array of objects to a file in Python

Viewed 2408

I know how to save an array of simple data (like float type values) to a file using numpy.save and numpy.savez.

I also know how to save a single object to a file using the pickle module, although I have not tested it yet.

The question is: How can I save (and load) an array of objects to a file? Can I combine the two methods exposed above in order to achieve this? Is there a better way?

1 Answers

If you know how to pickle a single object, to pickle some amount of objects you can create a structure that contains all these objects (list, set, dict with these objects as values for example, or your own class which contains them in some way) and this structure will be a single object awailable to pickle.

Related