Python Pickle Example | DigitalOcean (2024)

In this tutorial we will be discussing about Python Pickle Example. In our previous tutorial, we discussed about Python Multiprocessing.

Python Pickle

Python Pickle is used to serialize and deserialize a python object structure. Any object on python can be pickled so that it can be saved on disk. At first Python pickle serialize the object and then converts the object into a character stream so that this character stream contains all the information necessary to reconstruct the object in another python script. Note that the pickle module is not secure against erroneous or maliciously constructed data according to the documentation. So, never unpickle data received from an untrusted or unauthenticated source.

Python Pickle dump

In this section, we are going to learn, how to store data using Python pickle. To do so, we have to import the pickle module first. Then use pickle.dump() function to store the object data to the file. pickle.dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode. And the third argument is the key-value argument. This argument defines the protocol. There are two type of protocol - pickle.HIGHEST_PROTOCOL and pickle.DEFAULT_PROTOCOL. See the sample code to know how to dump data using pickle.

import pickle# take user input to take the amount of datanumber_of_data = int(input('Enter the number of data : '))data = []# take input of the datafor i in range(number_of_data): raw = input('Enter data '+str(i)+' : ') data.append(raw)# open a file, where you ant to store the datafile = open('important', 'wb')# dump information to that filepickle.dump(data, file)# close the filefile.close()

The following program will prompt you to enter some input. In my case, it was like this. Python Pickle Example | DigitalOcean (1)

Python Pickle load

To retrieve pickled data, the steps are quite simple. You have to use pickle.load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Simple! Isn’t it. Let’s write the code to retrieve data we pickled using the pickle dump code. See the following code for understanding.

import pickle# open a file, where you stored the pickled datafile = open('important', 'rb')# dump information to that filedata = pickle.load(file)# close the filefile.close()print('Showing the pickled data:')cnt = 0for item in data: print('The data ', cnt, ' is : ', item) cnt += 1

The output will be the following:

Showing the pickled data:The data 0 is : 123The data 1 is : abcThe data 2 is : !@#$

Python Pickle Example

I made a short video showing execution of python pickle example programs - first to store data into file and then to load and print it. Python Pickle Example | DigitalOcean (2) As you can see that the file created by python pickle dump is a binary file and shows garbage characters in the text editor.

Important Notes on Python Pickle

Few important points about python pickle module are:

  1. The pickle protocol is specific to Python - it’s not guaranteed to be cross-language compatible. This means you most likely can’t transfer the information to make it useful in other programming languages.
  2. There is also no guarantee of compatibility between different versions of Python because not every Python data structure can be serialized by the module.
  3. The latest version of the pickle protocol is used by default unless you manually change it.
  4. Last but not least, the pickle module is not secure against erroneous or maliciously constructed data according to the documentation.

So, that’s all about python pickle example. Hope that you understand well. For any further query please use the comment section. :) Reference: Official Documentation

Python Pickle Example | DigitalOcean (2024)
Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 6387

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.