JSON and pickle modules, two modules for serialization
JSON module for conversion between a string and a Python data type
Pickle Module for conversion between Python-specific types and Python data types
Two modules, all available with dumps,dump,loads,load 4 functions
1 ImportJSON2s ='{"Key1": "Value1", "Key2": "Value2"}' #==> use JSON modules to convert strings to other data types, the quotation marks in the string must be enclosed in double quotation marks3ret = Json.loads (s)#==> loads to other data types by string4 Print(Ret,type (ret))5 6RET = json.load (open ('Ethan.txt','R'))#==> Convert a document (internally a string format) to another Python data type7 Print(Ret,type (ret))#A dictionary-style string in the ==> document8 9L ='[11,22,3,56,75]'Tenresult =json.loads (L) One Print(Result,type (result)) A #Summary: - #json.loads () converts strings that resemble dictionaries, lists, and tuples into dictionaries, lists, tuples - #json.load () converts a document (a string that resembles a dictionary, list, tuple) to a dictionary, list, tuple the -Di = {"Key1":"value1","Key2":"value2"} -ret = Json.dumps (DI)#==> Converting dictionaries, lists, and tuples to string formats - Print(Ret,type (ret)) + -Json.dump (Di,open ('Ethan.txt','A +'))#==> Convert dictionaries, tuples, lists into string formats and write to documents + A ImportPickle at -D = {'name':'Ethan',' Age': 28} -ret = Pickle.dumps (d)#==> Pickle Converting dictionaries, tuples, and lists into binary - Print(Ret,type (ret)) - -L = [11,22,3,45,54] inres =pickle.dumps (L) - Print(RES) to +Pickle.dump (D,open ('Ethan.txt','AB'))#==> converting dictionaries, tuples, and lists to binary write documents - the #Note dump load do not run together, will error, step by step to * $f = open ('Ethan.txt','RB')Panax NotoginsengR = Pickle.loads (F.read ())#==> converting binary into dictionaries, lists, tuples - Print(r)
Python modules (JSON and pickle modules)