How to Use Pickle to Save and Load Variables in Python?


Pickle is a Python module that is used for data serialization ie. converting data into a byte stream. Pickle allows developers to save and load variables from memory to disk, ensuring data integrity and easy retrieval. In this article, we will explore how to utilize Pickle effectively to save and load variables in Python.

Understanding Pickle

Pickle is a built-in module in Python that enables object serialization, which refers to the process of converting objects into a byte stream. The byte stream can be stored as a file, transferred over the network, or even persisted in a database. Pickle allows the serialization and deserialization of complex Python objects, including lists, dictionaries, classes, and more. It provides a simple and efficient way to save variables in their entirety, preserving their state and structure.

Algorithm

A general algorithm for saving and loading data using pickle is as follows:

  • Import the Pickle module using import pickle.

  • To save variables:

    • Open a file in binary mode using open(file_path, 'wb').

    • Serialize and write the variable to the file using pickle.dump(variable, file).

    • Close the file using file.close() or utilize the with statement.

  • To load variables:

    • Open the file in binary mode using open(file_path, 'rb').

    • Deserialize and retrieve the variable from the file using pickle.load(file).

    • Close the file using file.close() or utilize the with statement.

Steps for Saving and Loading Variables using Pickle

Step 1: Importing the Pickle module

Firstly, we need to import the Pickle module into our Python script or interactive session. This can be done using the following import statement:

import pickle

Step 2: Saving Variables

To save a variable using Pickle, we need to open a file in binary mode and write the serialized object into it. The general steps are as follows:

  • Open a file in binary mode using the open() function.

  • Use pickle.dump(variable, file) to serialize and write the variable to the file.

  • Close the file using the close() method.

Example

In the below example, we save a list named data to a file named data.pickle. The open() function is used with the mode wb to open the file in binary mode for writing. We then use pickle.dump(data, file) to serialize and write the content of data into the file.

# Step 1: Import Pickle
import pickle

# Step 2: Saving Variables
data = [1, 2, 3, 4, 5]
file_path = 'data.pickle'

# Open the file in binary mode
with open(file_path, 'wb') as file:
    # Serialize and write the variable to the file
    pickle.dump(data, file)


print("The variable 'data' has been saved successfully.")

Output

The variable 'data' has been saved successfully

Step 3: Loading Variables

To load a saved variable using Pickle, we need to follow these steps:

  • Open the file in binary mode using the open() function.

  • Use pickle.load(file) to deserialize and retrieve the variable from the file.

  • Close the file using the close() method.

Example

In the below example, we load the previously saved data variable from the file data.pickle. The open() function is used with the mode rb to open the file in binary mode for reading. Then, we utilize pickle.load(file) to deserialize and retrieve the content of the file, assigning it to the loaded_data variable

# Step 1: Import Pickle
import pickle

# Step 2: Saving Variables
data = [1, 2, 3, 4, 5]
file_path = 'data.pickle'

# Open the file in binary mode
with open(file_path, 'wb') as file:
    # Serialize and write the variable to the file
    pickle.dump(data, file)
# Step 3: Loading Variables
loaded_data = None

# Open the file in binary mode
with open(file_path, 'rb') as file:
    # Deserialize and retrieve the variable from the file
    loaded_data = pickle.load(file)

print("The variable 'data' has been loaded successfully.")

print("Loaded Data:", loaded_data)

Output

 The variable 'data' has been loaded successfully.
Loaded Data: [1, 2, 3, 4, 5]

Conclusion

In this article, we discussed how we can use pickle to save and load variables in python. Pickle is a versatile tool that simplifies data serialization in Python, offering a straightforward approach to saving and loading variables. By incorporating Pickle into your programming workflow, you can ensure the persistence and integrity of your data effortlessly.

Updated on: 13-Oct-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements