Unpickling
Unpickling means retrieving the data from the pickle
file. In the previous topic, you learned how to store (list, dictionary)
data in the pickle
file; now it's time to retrieve the stored data. In order to perform unpickling, we will use pickle.load()
. The pickle.load()
takes one file object as an argument.
Let's see the program:
import pickle pickle_file = open("emp1.dat",'r') name_list = pickle.load(pickle_file) skill_list =pickle.load(pickle_file) print name_list ,"n", skill_list
Let's understand the program line by line. The pickle_file = open("emp1.dat",'r')
syntax creates a file object in read mode. The name_list = pickle.load(pickle_file)
syntax reads the first pickled object in the file and unpickles it to produce the ['mohit', 'bhaskar', 'manish']
list. Similarly, skill_list =pickle.load(pickle_file)
reads the second pickled object in the file and unpickles it to produce the ['Python', 'Python', 'Java']
list.
Let's see the output to clear up any confusion:
Output of filepickle2...