1. Pickle Module
Python persisted storage data:
Python program run to get some strings, lists, dictionaries and other data, want to save for a long time, easy to use later, rather than simply put in memory shutdown power loss data. The pickle module in the Python module is handy, and he can convert objects into a format that can be transferred or stored.
Pickle module The process of converting any Python object into a system byte is called a serialized object.
Python's pickle module implements all data sequences and deserialization of Python. There is basically no big difference between functional usage and JSON modules, and the same approach is dumps/dump and loads/load. Cpickle is the C-language compiled version of the Pickle module relatively faster.
Unlike JSON, Pickle is not used for data transfer between multiple languages, it only acts as a method to persist Python objects or to transfer objects between Python programs, so it supports all Python data types.
ImportPickledata2= [1,2,3,4]det_str=pickle.dumps (data2)Print(DET_STR)#output: Outputs are in binary formatB'\x80\x03]q\x00 (k\x01k\x02k\x03k\x04e.'#storing data in a file after serializationf = open ('Test.txt','WB')#Pickle can only store data in binary format to filedata = {'K1':'python','K2':'Java'}f.write (pickle.dumps (data))#dumps writing a file after serializing the source dataf.close ()#deserializing read source dataImportPicklef= Open ('Test.txt','RB') da= Pickle.loads (F.read ())#deserialization using loadsPrint(DA)
The difference between dumps and dump,load and loads:
Dumps is the serialization of an object
Dump is to serialize the object and save it to a file
Loads deserializing a serialized string
Load reads and deserializes a serialized string from a file
Importpickledata1= [1,'a', 2,'b', 3,'C']pi= Pickle.dumps (data1)#Serializing ObjectsPrint(PI)Print(Pickle.loads (PI))#deserializing ObjectsF= Open ('Test1.txt','WB') Data2= ['py','th',' on', 123]pickle.dump (data2,f)#serializing an object to a filef = open ('Test1.txt','RB') Red= Pickle.load (f)#deserializing an object from a filePrint(red)
2. JSON module
JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).
Data1 = [1,'a', 2,'b', 3,'C']pi=json.dumps (data1)Print(PI)Print(Json.loads (pi)) F= Open ('Test1.txt','W')#Unlike Pickle, which does not use binary storage while using strDATA2 = ['py','th',' on', 123]json.dump (data2,f) F= Open ('Test1.txt','R') Red=json.load (f)Print(red)
3. The difference between JSON and pickle modules
1. JSON can only handle basic data types. The pickle can handle all Python data types.
2. JSON is used for character conversion between various languages. Pickle is used for the persistence of Python program objects or for object network transmission between Python programs, but different versions of Python serialization may have differences.
Serialization of Python3 (Pickle&json)