How-to Save Data in Python with Pickle
Introduction
There are times where you have some variable in Python that you need to persist between executions of the script. This data isn’t being exported, it isn’t being read by a human, it doesn’t need to be portable, it just has to survive between executions and no one else needs it.
You seem to be in quite a pickle.
Haha, you see - that’s what me and my countrymen call a joke. That’s because there’s a Python library called (for some reason) ‘pickle’ which lets you serialize and deserialize arbitrary data. Those super-serial words are just fancy talk for save and load for the purposes of this how-to.
So this article will show you how to use the pickle library to get yourself out of this… difficult situation.
This will be done on a Windows 10 PC with Python 3 (3.8.1 specifically).
Required Libraries
pickle is the only required library and as far as I know, it comes standard with Python.
Writing Serialized Data to a File
The basic examples from the website will work just fine:
Reading Serialized Data from a File
Here’s how they did it on the website:
Pickling Multiple Variables at Once
It didn’t take long before I figured out a wrinkle: I need to pickle multiple variables into one file and ensure that they get back into their proper variables when loaded. How do I do that?
This StackOverflow answer provides the answer: just dump them in order, then load them in order. Pickle will handle the rest.
Here’s the code they wrote: