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.
Functions used:
- 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)
Saving a variable:
- 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
myvar = [{ 'This' : 'is' , 'Example' : 1 }, 'of' ,
'serialisation' , [ 'using' , 'pickle' ]]
serialized = 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
myvar = [{ 'This' : 'is' , 'Example' : 2 }, 'of' ,
'serialisation' , [ 'using' , 'pickle' ]]
with open ( 'file.pkl' , 'wb' ) as file :
pickle.dump(myvar, file )
|
Loading a Variable:
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
binary_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.'
myvar = pickle.loads(binary_string)
print (myvar)
|
Output:
[{‘This’: ‘is’, ‘Example’: 1}, ‘of’, ‘serialisation’, [‘using’, ‘pickle’]]
The load() method loads a pickled file and returns a deserialized variable.
Example:
Python3
import pickle
with open ( 'file.pkl' , 'rb' ) as file :
myvar = pickle.load( file )
print (myvar)
|
Output:
[{‘This’: ‘is’, ‘Example’: 2}, ‘of’, ‘serialisation’, [‘using’, ‘pickle’]]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Feb, 2021
Like Article
Save Article