Pickle module in python 3

Pickle module in python 3 Tutorial: The pickle module in python is used for serializing and de-serializing an object structure. Any of the objects in Python can be pickled so that it can be saved on disk. The process will converts python objects as a list, dict into byte streams as 0 and 1 is called pickling. Then we convert the byte stream back into python objects by the process called unpickling. The character stream contains all the information that is necessary to reconstruct the object in another python script. They will provide a pickle module to store python objects in a file and get back called as storing object persistently. It will implement binary protocols for serializing and de-serializing a Python object structure. Python3 program is stored efficiently using the pickle module.  Here the module will translate an in-memory Python object into a serialized byte stream of string.

Importing the pickle

First, we try to import cPickle by giving an alias of “pickle”.
You can fall back on the native implementation in the pickle module then faster implementation.

python pickle Example

Try:
Import xpickle as pickle
Except:
Import pickle
Def storeData():
Omkar={‘key’:’omkar’,’name’:’omkar pathak’,’age’:21,’pay’:40000}
jagdish={‘key’:’jagdish’,’name’:’jagdish pathak’,’age’:50,’pay’:50000}
db={}
db=[‘omkar’]=omkar
db=[‘jagdish’]=jagdish
db.file=open(‘examplePickle’,’ab’)
pickle.dump(db,dbfile)
dbfile.close()
def loadData():
dbfilr=open(‘examplePickle’,’rb’)
db=pickle.load(dbfile)
for keys in db:
print(keys,’=>’,db[keys])
dbfile.close()
if__name__==’_main_’:
storeData()
loadData()

Output:-
Omkarpathak-Inspirion-3542:~/Documents/Python-
Programs$pythonP60_pickleModule.py
Omkar=> {‘age’:21,’name’:’omkar pathak’,’key’:’omkar’,’pay’:40000}
Jagdish= {‘age’:’50’,’name’:’jagdish pathak’,’key’:’jagdish’,’pay’:50000}

Pickling without a file

Example:-
Omkar = {‘key’: 'Omkar', ‘name’: 'Omkar Pathak', 
‘age’: 21, ‘pay’: 40000}
Jagdish = {‘key’: 'Jagdish', ‘name’: 'Jagdish Pathak',
‘age’: 50, ‘pay’: 50000}
db = {}
db['Omkar'] = Omkar
db['Jagdish'] = Jagdish
b = pickle. dumps (db)      
MyEntry = pickle. loads (b)
print(myEntry)
The pickle module will implement the algorithm for turning objects into series.
The cpickle implements the same algorithm in and allows users to subclass from pickle.

  

Encoding and decoding data in string

Example:-
Try:
Import cpickle as pickle
Except:
Import pickle
Import pprint
Data= [{‘a’:’A’,’b’:2,’c’:3.0}]
Print ‘DATA:’
Pprint.pprint (data)
Data_string=pickle. dumps (data)
print ‘PICKLE:’ data_string

Working with streams:-

The dumps () and loads () the pickle provide functions for working streams.
We write multiple objects to stream and read them from the stream.
Example:-
Try:
Import cpickle as pickle
Except:
Import pickle
Import pprint
From stringIO import stringIO
Class SimpleObject (object):
Def_init_ (self, name):
Self.name=name
1=list (name)
1. reverse ()
Self.name_backwards=”.join (1)”
Return
Data= []
Data. append (simpleObject (‘pickle’))
Data. append (simpleObject (‘last’))
Out_s=stringIO ()

Pickle exceptions:-

The pickle.picklingError:-if pickle object that doesn’t support pickling this exception is raised.
pickle.unpicklingError:-the file contains bad or corrupted data.

EOFError
TypeError
ValueError
AttributeError
ImportError
IndexError
EOFError:-the end of file is detected and this exception is raised.

Advantages:-
It will come handy to save the data and easy to use.
It has less weight and requires several lines of code.
Recursive object: - The pickle keeps the track of the objects in serialize.
Object sharing:-it is the same as self- referencing objects pickle that stores the object once and ensures references point to the master copy.

Disadvantages:-
No other language than python will reconstruct the object.
The risk is present in data from malicious sources.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India