first, the use
We need to serialize the in-memory data, that is, when writing to a file, the type can only be a string or binary type. But if we want to serialize more complex data types, such as lists, dictionaries, or functions, we need to use JSON or pickle.
Second, JSON serialization1. Serialization of dumps and loads deserialization
Dumps converting data types to strings
Import jsoninfo = { ' name ': ' The Count of Monte Cristo ', ' type ': ' Movie '}data = json.dumps (info) print ( Type (data) # output {"name": "The Count of Monte Cristo", "type": "Movie"}<class ' str ' >
Loads converting a string to a data type
Import Jsonget_info = json.loads (data) print (get_info[' name ']) print (get_info) print (Type (get_info)) #输出The Count of Monte cristo{' name ': ' The Count of Monte Cristo ', ' type ': ' Movie '}<class ' dict ' >
2.dump Serialization and load deserialization
Dump converts the data type into a string and stores it in a file
Import jsoninfo = { ' name ': ' The Count of Monte Cristo ', ' type ': ' Movie '}with open ("Test.txt", "W", encoding= "utf- 8 ") as F: Json.dump (info, f) # The first parameter is an in-memory data object, the second parameter is the file handle # written to the contents of the file {" name ":" The Count of Monte Cristo "," type ":" M Ovie "}
Load converts a file from a string to a data type
Import Jsonwith Open ("Test.txt", "R", encoding= "Utf-8") as f: data_from_file = Json.load (f) Print (data_from_file[' Name ']) print (data_from_file) print (Type (data_from_file)) #输出The Count of Monte cristo{' name ': ' The Count of Monte Cristo ' , ' type ': ' Movie '}<class ' dict ' >
3.json serialization of a function
Import jsondef Test (name): print ("hello,{}". Format (name)) info = { ' name ': ' The Count of Monte Cristo ', ' Type ': ' Movie ', ' func ': Test}data = json.dumps (info) #输出 File "g:/python/untitled/study6/json&pickle module. Py", Line a <module> data = json.dumps (info) File "G:\python\install\lib\json\__init__.py", line +, in Dumps return _default_encoder.encode (obj) File "G:\python\install\lib\json\encoder.py", line 198, in encode chunks = Self.iterencode (o, _one_shot=true) File "G:\python\install\lib\json\encoder.py", line-up, in Iterencode return _iterencode (o, 0) file "G:\ python\install\lib\json\encoder.py ", line 179, in default raise TypeError (Repr (o) +" was not json serializable ") Type Error: <function Test at 0x0000021b13c57f28> was not JSON serializable
1, JSON can only handle simple data types, such as: dictionaries, lists, strings, etc., can not handle complex data types such as functions.
2,json is common in all languages, all languages support JSON, if we need Python and other languages to interact with the data, then in JSON format
iii. serialization of Pickle
The usage of pickle is the same as above, but the data type after pickle serialization is binary, and pickle can only be used in Python.
1.dumps && Loads
Import pickledef Test (name): print ("hello,{}". Format (name)) info = { ' name ': ' The Count of Monte Cristo ', ' Type ': ' Movie ', ' func ': Test}data = pickle.dumps (info) print (data) print (type data) #输出b ' \x80\x03}q\x00 (x\x04\ X00\x00\x00nameq\x01x\x19\x00\x00\x00the Count of Monte cristoq\x02x\x04\x00\x00\x00typeq\x03x\x05\x00\x00\ x00movieq\x04x\x04\x00\x00\x00funcq\x05c__main__\ntest\nq\x06u. ' <class ' bytes ' >
Import Pickleget_data = pickle.loads (data) get_data[' func '] (' cat ') print (get_data) #输出hello, cat{' name ': ' The Count of Monte Cristo ', ' type ': ' Movie ', ' func ': <function test at 0x00000235350a7f28>}
2. Dump && Load
Import pickledef Test (name): print ("hello,{}". Format (name)) info = { ' name ': ' The Count of Monte Cristo ', ' Type ': ' Movie ', ' func ': Test}with open (' Test.txt ', ' WB ') as F: Pickle.dump (Info, f) # Write the contents of the Test.txt file?} Q (X typeqx movieqx funcqc__main__testqx nameqx the Count of Monte Cristoqu.
Import Picklewith Open (' test.txt ', ' RB ') as f: get_data = Pickle.load (f) Print (Get_data) # output {' name ': ' The Count of Mon Te Cristo ', ' func ': <function test at 0x000001ba2ab4d510>, ' type ': ' Movie '}
Summarize:
- JSON values support simple data types, and pickle supports all data types.
- Pickle can only support serialization and deserialization of Python itself and cannot be used as a data interaction with other languages, and JSON can.
- Pickle serializes the entire data object, so when deserializing the function, the logic in the function body changes, and is followed by the function body of the heart.
Serialization of Json&pickle