python pickle example | python pickle tutorial | part 1

Namaste Friends,






                                                      This is our first part of python pickle module tutorial series. and in this tutorial i am going to show you practical examples of python pickle module.

so, keep reading ....

if you are new visitor then, first check our Introduction Part Of Python pickle tutorial series.

or directly check our index for more interesting tutorials.

so let's start



Input : [1]  


#
# ==================================================
#          PYTHON PICKLE MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.com
#            
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version   : python 2.7.12
# Editor           : ipython notebook
#

Output : [1]  



Input : [2]  

# import pickle module
import pickle

Output : [2]  



Input : [3]  

#
# here, i am try to import cPickle
# becuase cPickle is written in C language.
# and pickle is written in pure python
# that's why cPickle Is Faster Then Pickle
# cPickle and pickle module functions and
# classes works same.
#
try:
    # first trying to import cPickle as pickle
    import cPickle as pickle
except:
    # if cPickle is not installed
    # then, import pickle
    import pickle
    
#

Output : [3]  



Input : [4]  

# 
# creating a list on for example purpose
#
storelist = ['cat','rat','dog','cow']

Output : [4]  



Input : [5]  

#
# print list
#
print storelist

Output : [5]  



Input : [6]  

#
# Here, pickling object into string.
# and storing that into a new variable
#
pickled_data = pickle.dumps(storelist)

Output : [6]  



Input : [7]  

#
# print pickled data from variable
#
print "List Converted In Pickled String : ", [pickled_data] 

Output : [7]  



Input : [8]  

#
# here, unpickling object from string
# and saving unpickled object into new variable
#
un_pickle_data = pickle.loads(pickled_data)

Output : [8]  



Input : [9]  

#
# here Comparing Object 
#
print "Before Pickling ",storelist
print "After Pickling And UnPickling ", un_pickle_data

Output : [9]  



Input : [10]  

#
# creating new dictonery object only for example purpose
#
store_dict = {"rat":"cat",
             "cat":"dog",
             "dog":"wolf",
             "wolf":"lion"}

Output : [10]  



Input : [11]  

#
# print dictonery object
#
print store_dict

Output : [11]  



Input : [12]  

#
# pickled dictonery object into string
#
pickled_data = pickle.dumps(store_dict)

Output : [12]  



Input : [13]  

#
# print pickled string
#
print [pickled_data]

Output : [13]  



Input : [14]  

#
# creating a file handler. because here we will
# save pickled string into a txt file.
#
txt_file = open("pickled_data.txt",'wb')

Output : [14]  



Input : [15]  

#
# write pickled string into txt file
#
txt_file.write(pickled_data)

Output : [15]  



Input : [16]  

#
# Close Txt File
#
txt_file.close()

Output : [16]  



Input : [17]  

#
# Now, Again Open Same Txt File
#
get_data_from_txt = open("pickled_data.txt",'rb')
#
# Read Data From Txt File 
read_data = get_data_from_txt.read()

Output : [17]  



Input : [18]  

#
# Print data recoverd from txt file
print [read_data]
#
# unpickled string into dictonery object again
#
un_pickled_data = pickle.loads(read_data)

Output : [18]  



Input : [19]  

#
# Print Unpickled Dictonery
#
print [un_pickled_data]

Output : [19]  



Input : [20]  

#
# Let's try something more interesting
# Here i am creating a class that can multiply
# two numbers provide by the user
#
class multiply:
    
    def __init__(self, x,y):
        self.x = x
        self.y = y
        
    def get(self):
        return self.x * self.y

Output : [20]  



Input : [21]  

#
# Let's check 
#
multiply_class = multiply(2,5)

Output : [21]  



Input : [22]  

#
# print answer
#
print multiply_class.get()

Output : [22]  



Input : [23]  

#
# Open New txt file for saving multiply class pickled object
#
save_pickled_data = open("save_pickled_data.txt",'wb')

Output : [23]  



Input : [24]  

#
# use pickle.dump(object, file) for directly writing data
# into file
#
pickle.dump(multiply_class, save_pickled_data)

Output : [24]  



Input : [25]  

#
# Now, close File
#
save_pickled_data.close()

Output : [25]  



Input : [26]  

#
# open same file as read only
#
save_pickled_data = open("save_pickled_data.txt",'rb')

Output : [26]  



Input : [27]  

#
# use pickle.load(file) for directly reading
# pickled data from file and unpickling that data
#
multiply_class = pickle.load(save_pickled_data)

Output : [27]  



Input : [28]  

#
# here, we got multiply class object again
# after pickling and unpickling
#
multiply_class

Output : [28]  

<__main__.multiply instance at 0x7fb028097a28>

Input : [29]  

#
# let's check it
#
print multiply_class.get()

Output : [29]  



Input : [30]  

#
# let's check class object working correctly or not
#
multiply_class.x=20
multiply_class.y=30

Output : [30]  



Input : [31]  

#
# print answer
#
print multiply_class.get()

Output : [31]  



Input : [32]  

# yeah, its working.
#
# so, as you can see python pickle module is
# really very easy to use and very helpful
# for saving data and class objects. etc
# as string.
#
#

Output : [32]  



I think, this is enough for first part.

now, for next part click here


So, stay tunned with us.




Thanks For Reading.

For More Update, Visit Our Blog Regularly. 
, Subscribe Our Blog, 
Follow Us and share it.
For Any Type of Suggestion, Help Or Question
Contact me:
S.S.B
surajsinghbisht054@gmail.com
or Comment Below

Share this

Related Posts

Previous
Next Post »