Python
pickle module is used for serializing and deserializing the object
stream.
The
pickle module consist of two major operation :-
Pickling
---------> conversion of object stream into byte stream
Unpickling
-----> reverse of pickling , byte stream into object stream.
Once
the object stream is converted into byte stream , the pickle file has
all the necessary information reinstate the earlier state.
When
to use pickle ?
1.)
when we need the data to be
persisted on the disk and to use it on the later stage of the
program.
2.)
when we need to send the
data over the network protocol like TCP connection.
3.)pickling
is normally used in Machine learning algorithm where
we need the same data set to prediction at later stage .
Limitation
of pickle :- The major
disadvantage of pickle module is that it is programming language
dependent and this module can work well only with Python.
Difference
between pickle and JSON :- The
pickle module is often comapre to the JSON
but there are significant difference between pickle and JSON.
-
JSON is human readable while pickle is not human readable.
-
JSON file can work across different platform and is compatible with different programming language while pickle is python specific.
-
JSON is text serialization format while pickle is binary serialization format.
Code
Snippet :-
Created on Sat Mar 2 23:11:30
2019
@author: sangam
"""
import pickle
data_to_be_pickle =
['tad','five','kyle','Ram']
dump_filename = 'file_pickle'
outfile =
open(dump_filename,'wb')
data_to_be_dumped =
pickle.dump(data_to_be_pickle,outfile)
outfile.close()
infile =
open(dump_filename,'rb')
data_to_be_unpickle =
pickle.load(infile)
print(data_to_be_unpickle)
No comments:
Post a Comment