Before, in learning Python, always confused the Pickle and JSON module serialization and the use of the inverse of the example, the recent idle time to re-examine the two modules, is basically to understand the difference between them.
Two modules for serialization,
- JSON, used to convert between string and Python data types
- Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
Look at the following example, maybe you can understand the difference between them
ImportPickledata= ['AA','BB','cc']#dumps converts data into a string that is known only in Python language in a special formP_str =pickle.dumps (data)Print(P_STR)#loads converting pickle data to Python's structureMes =pickle.loads (P_STR)Print(MES)#dump converts the data into a string that is known only in the Python language in a special form and writes the fileWith open ('TMP.PW','WB') as F:pickle.dump (data,f)#load reads data from the data file and transforms it into a python structureWith open ('TMP.PW','RB') as F:Print(Pickle.load (f))
The dump and load operations in JSON and pickle modules are actually the processing of encoding and decoding data.
- encoding : Converting a python object encoding into a JSON string json.dumps ()
- decoding : Converting JSON format strings to Python objects json.loads ()
Serialization and deserialization in Python