Linux

Python Pickle

Text files are convenient because you can read, write and append them with any text editor, but they are limited to storing a series of character. Sometimes you may want to store more complex information like list, dictionary.
Here we will use Python pickle. Python Pickle is used to store more complex data like list, dictionary. Let us discuss with the help of example.
import cPickle
name = ["Mohit", "Ravi","Bhaskar"]
skill = ["Python", "Java", "java"]
pickle_file = open("pick.dat","w")
cPickle.dump(name,pickle_file)
cPickle.dump(skill,pickle_file)
pickle_file.close()
Out-put
Python pickle file example
Python Pickle Example
Program seems little bit complex let us discuss line by line.
import cPickle
The cPickle module allows you to pickle and store more complex data in file.
Python also has a pickle module, which works like the cPickle module, pickle is written in Python while cPickle is written in C. cPickle is much faster than cPickle . However it does not support subclassing of the Pickler() and Unpickler() classes, because in cPickle these are functions, not classes.

name = ["Mohit", "Ravi","Bhaskar"]
skill = ["Python", "Java", "java"]


These are two lists which we have to store.

pickle_file = open("pick.dat","w")

Create a file object in write mode as we have done in previous File chapter.

cPickle.dump(name,pickle_file)
cPickle.dump(skill,pickle_file)


i want to pickle and store the two list, name and skill, in the file pick.dat using cPickle.dump() functiom. The function requires two arguments first the data to pickle and second the file to store it.

pickle_file.close()

Finally program close the file.
So this code pickles the list referred to by name and writes the whole things as one object to the file pick.dat. Next, the program pickles the list referred to by skill and writes the whole thing as one object to the file.

Reading data from file and Unpickling

Now time to retrieve and unpickle the two lists with the cPickle.load() function. The function takes one argument as file from which to load next pickled object.
import cPickle
pickle_file = open("pick.dat",'r')
name = cPickle.load(pickle_file)
skill=cPickle.load(pickle_file)
print name ,"\n", skill
Out-put
python unpickle the list from the file
unpickle the list from the file
pickle_file = open("pick.dat",'r')
Create a file object in read mode. name = cPickle.load(pickle_file)
skill=cPickle.load(pickle_file)

The program reads the first pickled object in the file, unpickles it to poroduce list ['Mohit', 'Ravi', 'Bhaskar'] and assigns the list to name. Next the program reads the next pickled object from the file unpickles it to produce the list ['Python', 'Java', 'java'] and assigns the list to skill.
Let us discuss another example.
import cPickle
pickle_file = open("pick.dat",'r')

skill=cPickle.load(pickle_file)
print skill
Out-put
Python unpickle the random list from the file
unpickle the Random list from the file
The above result is not what we are expecting. We are trying to fetching key skill but in output key name is returned. Now we can say pickle stores and retrieves the list sequentially. You can pickle number, string, list, Tuple and dictionary. You can not access keys randomly. In next section you will see how to access key randomly.




admin