JSON & Pickle
JSON: Typically used for serialization and deserialization between different programming languages, the core of which is the string, which is the conversion between the string and the basic data type of Python
The JSON module provides four functions:dumps, dump(serialization, Save), loads(deserialization, read), load
#json.loads is used to convert strings in dictionaries, lists, and elements into corresponding dictionaries, lists, tuples#for the contents of the dictionary, there must be ""#Example:L ="[11,22,33,44]"ImportJsonresult=json.loads (L)Print(Result,type (result))#output: [One, all, <class] ' list ' >json.dumps ()#convert Python's basic data type to stringUser_list = [11,22,33,44]Importjsons=json.dumps (user_list)Print(S,type (s))
Pickle: A basic data type that is specific to Python and a conversion between sequences that only Python can recognize, which is often the bytes type. Pickle is powerful in the ability to serialize code, class
The Pickle module provides four functions:dumps, dump(serialization, Save), loads(deserialization, read), Load
ImportPickledata= {"K1": 123,"K2":'Hello'}#pickle.dumps converts data into unique Python-specific types through special formsresult =pickle.dumps (data)Print(result, type (result))#B ' \x80\x03}q\x00 (x\x02\x00\x00\x00k1q\x01k{x\x02\x00\x00\x00k2q\x02x\x05\x00\x00\x00helloq\x03u. ' <class ' bytes ' >#pickle.dump converts data into unique Python-specific types and writes to a fileWith open ('Dbb.txt','WB') as F:pickle.dump (data, F)#pickle.loads to restore Python-specific types to the original dataNew_data =pickle.loads (Result)Print(New_data, type (new_data))#{' K1 ': 123, ' K2 ': ' Hello '} <class ' Dict ' >#Pickle.load read from a file that only Python-specific types are restored to the original dataWith open ('Dbb.txt','RB') as F:new_data=pickle.load (f)Print(New_data, type (new_data))#{' K1 ': 123, ' K2 ': ' Hello '} <class ' Dict ' >
JSON & Pickle