Skip to content

How to use Pickle module to store data in Python

Last Updated on 2020-11-18 by Clay

Python is the most popular programming language!

Introduction

Hello! Today I want to record the "pickle" module in Python. The "pickle" module is very famous module in Python, its usage is like another Python package "Json", it can compress the data and store it. If we want to load the data we store, we can easily call the "load()" function to restore our session.

However, because it is specifically designed for Python, so if you need to develop across program languages, maybe "pickle" not be a suitable module for storing data.

In this case, "Json" is a good lightweight data storage format.

Let's take a brief look at an example that actually uses pickle.


How to use pickle

Since "pickle" is a built-in module in Python, so we don't need to install it with additional instructions, just import it in the code.

let's take a look for a sample code:

import pickle

my_dict = {
    'a': 1,
    'b': 2,
    'c': 3,
}

with open('test.pickle', 'wb') as f:
    pickle.dump(my_dict, f)

We can use "dump()" function to store our data. And then we can use "load()" function to load our data stored by "pickle".

# Load
with open('test.pickle', 'rb') as f:
    new_dict = pickle.load(f)

print(new_dict)

Output:

{'a': 1, 'b': 2, 'c': 3}

It's very easy!

Of course you may think: how is this different from "Json"? Aren't all stored data and read? Even the functions are exactly the same!

But this is not the case, because "pickle" is specifically for Python, we can restore our "Python session".

class dog:
    def __init__(self, color):
        self.color = color

    def color(self):
        return self.color()

puppy = dog('black')


with open('test.pickle', 'wb') as f:
    pickle.dump(puppy, f)

Suppose we defined a Python class: "dog", and we gave a attribute "color" for it. And then we create a object "puppy" and assign "black" color attribute.

We can stored "puppy" by "pickle" directly, it is a advantage of pickle. Json only supports some Python data types.

# Load
with open('test.pickle', 'rb') as f:
    my_dog = pickle.load(f)

print(my_dog.color)

Output:

black

As we can see, "pickle" load the class we defined directly, and print the color of "my_dog", is it convenient?

But I heard that Json reads faster than pickle. So if you are storing and loading a large amount of data, it may be better use Json.

I also hope that in the future, I can take a relatively large amount of data to do my own experiments.


References


Read More

Leave a Reply