JSON module:
JSON: Information exchange (conversion of strings) to any language
After JSON conversion, all types can be converted to double quotation marks before being converted to STR
For example: file reads and writes:
1 #Write file:2 ImportJSON3f = open ("Demo.txt","W")4DIC = {"name":"g_l"}5Dic_str =json.dumps (DIC)6 f.write (DIC_STR)7 Print(Type (DIC_STR))8 Print(DIC_STR)
1 # Read file: 2 import JSON 3 f = open ( " Demo.txt ", " R " ) 4 f_read = F.read () 5 f_read = Json.loads (f_read) 6 print (type (f_read)) 7 print (Type (f_read))
Load and dump for file operations only: "DiC f reference above two code"
Json.dump (DIC,F)
Json.load (f)
Packle Modules: (Support functions, classes, etc.)
Serialization: The process of turning a variable (object) from memory into a storage or transfer is called serialization
1 ImportPickle2DIC = {"name":"Alex","name":"g_l"," Age": 18}3 #Print (Type (DIC))4j =pickle.dumps (DIC)5 Print(Type (j))#byte type6 Print(j)7f = open ("Serialized Object _pickle",'WB')8Pickle.dump (DIC,F)#f.write () #不可读文件9 f.close ()Ten ##---deserialization: One ImportPickle Af = open ("Serialized Object _pickle",'RB') -Date = Pickle.loads (F.read ())#---->date = Pickle.load (f) - Print(Type (date[" Age"])) the Print(date[" Age"])
View Code
Shelve module:
Compared to the Pickle module, it is simpler and has only one open function;
Returns an object similar to a dictionary;
Readable and writable;
Key must be a string;
Value is all the types that Python can support
JSON module, pickle module (serialized) Shelve module