Wednesday, August 6, 2014

Python Pickle

How To Use Python Pickle


What is pickle module?
=> The pickle module provides serializing and de-serializing a Python object structure. “Pickling”  = Serializing 
“unpickling” = DeSerializing

To Pickle,       dump() function is used
To Un-Pickle, load() function is used 
Example:

import pickle

d1 = {}

d1['name'] = 'kuldeep'
d1['school'] = 'sjsu'
d1['city'] = 'san jose'
d1['id'] = 252666


print "before pickling \n",d1

#PICKLING
f1 = open("pickled_data", "wb")
pickle.dump(d1, f1)
f1.close()

#UNPICKLING
f1 =open("pickled_data", "rb")
d1 = pickle.load(f1)
f1.close()

print "after pickling \n", d1


-------------------------------------------------------------------


output:


before pickling 
{'city': 'san jose', 'school': 'sjsu', 'name': 'kuldeep', 'id': 252666}
after pickling 
{'city': 'san jose', 'school': 'sjsu', 'name': 'kuldeep', 'id': 252666}

-------------------------------------------------------------------



SOURCE = 

No comments:

Post a Comment