How to use Pickle to save and load Variables in Python?

25 Feb, 2021
Serialization is a technique used to save the state of an object from any process. We can later use this state by deserialization, to continue the process. Pickle is a python module that makes it easy to serialize or save variables and load them when needed. Unlike JSON serialization, Pickle converts the object into a binary string. JSON is text specific, but Pickle is python specific, and it can serialize the custom classes which JSON fails to serialize. Due to this feature, it is heavily used in training machine learning models. This article discusses how variables can be saved and loaded in python using pickle.
In python, dumps() method is used to save variables to a pickle file.
Syntax:
pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
In python, loads() is used to load saved data from a pickled file
Syntax:
pickle.loads(data, /, *, fix_imports=True, encoding=”ASCII”, errors=”strict”, buffers=None)
Method 1: Passing the variable
In dumps() method, we can pass the variable, and it will return us the binary string for the same. We can then transmit it to other python modules or save in a database.
Example:
Python3
import pickle  # Create a variablemyvar = [{'This': 'is', 'Example': 1}, 'of',         'serialisation', ['using', 'pickle']]  # Use dumps() to make it serializedserialized = pickle.dumps(myvar)  print(serialized)
Output:
b’\x80\x04\x95K\x00\x00\x00\x00\x00\x00\x00]\x94(}\x94(\x8c\x04This\x94\x8c\x02is\x94\x8c\x07Example\x94K\x01u\x8c\x02of\x94\x8c\rserialisation\x94]\x94(\x8c\x05using\x94\x8c\x06pickle\x94ee.’
Method 2:  We can directly save the variable in a file itself.
Example:
Python3
import pickle  # Create a variablemyvar = [{'This': 'is', 'Example': 2}, 'of',         'serialisation', ['using', 'pickle']]  # Open a file and use dump()with open('file.pkl', 'wb') as file:          # A new file will be created    pickle.dump(myvar, file)
Method 1: 
The loads() method takes a binary string and returns the corresponding variable. If the string is invalid, it throws a PickleError.
Example:
Python3
import pickle  # This is the result of previous codebinary_string = b'\x80\x04\x95K\x00\x00\x00\x00\x00\x00\x00]\x94(}\x94(\x8c\x04This\x94\x8c\x02is\x94\x8c\x07Example\x94K\x01u\x8c\x02of\x94\x8c\rserialisation\x94]\x94(\x8c\x05using\x94\x8c\x06pickle\x94ee.'  # Use loads to load the variablemyvar = pickle.loads(binary_string)  print(myvar)
Output:
[{‘This’: ‘is’, ‘Example’: 1}, ‘of’, ‘serialisation’, [‘using’, ‘pickle’]]
Method 2: 
The load() method loads a pickled file and returns a deserialized variable.
Example:
Python3
import pickle  # Open the file in binary modewith open('file.pkl', 'rb') as file:          # Call load method to deserialze    myvar = pickle.load(file)      print(myvar)
Output:
[{‘This’: ‘is’, ‘Example’: 2}, ‘of’, ‘serialisation’, [‘using’, ‘pickle’]]
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function