| by Arround The Web | No comments

How to Read Pickle File Python?

Pickling data and storing it inside a file is a very useful concept and technique that actually helps the user to manage vast amounts of data. However, it not only helps in managing huge amounts of data but also helps the program load the data fast thus saving lots of crucial time.

This post is going to show the methods for reading data from a pickle file and displaying it on the terminal.

Pre-requisite: Create Pickle File

To read data from a pickle file, there must first exist a pickle file. Therefore, in this step, you are going to first create a new pickle file. If you already have an existing pickle file then you can skip this step. Use the following lines of code, to create a dictionary of a person in Python and store it inside a pickle file:

import pickle

person1 = {"name": "Marcus King", "Age": "22", "Profession" : "Author"}

pickle.dump(person1, open("person1.p", "wb"))

The above-mentioned code snippet will:

  • Create a dictionary named “person1”.
  • Create a new file named “person1.p”.
  • Write the pickle data in the person1.p file.

Once the file has been created you can start working on the different methods or reading this pickle file data.

Method 1: Using pickle.load() method from Pickle Package

The pickle library contains a method which is the load() method which reads the pickled data from the file specified in its path. To demonstrate this start by importing the pickle library and opening up the file with read access using the following lines:

import pickle
pickleFile = open("person1.p","rb")

After that, use the load() method to load the pickled data and then store it inside a variable:

personInfo = pickle.load(open("person1.p", "rb"))

Print the data stored in the variable to verify that the pickle data has been read correctly:

print (personInfo)

The complete code for reading pickle data from the file created in the prerequisite step:

import pickle

pickleFile = open("person1.p","rb")

personInfo = pickle.load(open("person1.p", "rb"))
print (personInfo)

Running this code will result in the following output on the terminal:

You have successfully read the data from a pickle file.

Method 2: Using read_pickle() method from Pandas Package

This method requires the use of the Pandas package as it contains the method “read_pickle()”. To use this, import the pandas library and open the pickle file with read access using the following lines:

import pandas as pd

pickleFile = open("person1.p","rb")

Use the read_pickle() method and pass in the file as the argument and then store the result in an “obj” variable:

obj = pd.read_pickle(pickleFile)
print (obj)

Complete code for this method is as:

import pandas as pd

pickleFile = open("person1.p","rb")

obj = pd.read_pickle(pickleFile)
print (obj)

Executing this code will produce the following result on the terminal:

You have successfully read the pickle file using the Pandas library

Conclusion

To read the pickle file in Python, simply use the “load()” method from the pickle package or use the “read_pickle()” method from the pandas library. Both of these methods will require you to first open up the file using the open() method in the read access mode and then use the methods on them. This post explained the process of reading data from a pickle file.

Share Button

Source: linuxhint.com

Leave a Reply