JSON as a medium for data interaction between different languages, has gradually replaced the previous XML in the present, looking at a wave of Python operation JSON
1 #coding = ASCII2 3 ImportJSON4 ImportPickle5 ImportSYS6 ImportOS7 " "8 Practice Json,pickle9 JSON-type data enables data interaction between different languages,Ten Python is introduced in Python3 to implement data in the form of dictionaries, lists, etc. One packaged as a string, it is serialized and implemented using Eg.json.dump (LIST,F) or F.write (Json.dumps (list)). A deserialization: The text content of the string format is parsed into the corresponding original format data to achieve the operation, - use Eg.json.load (f) or Json.loads (F.read ()) - Pickle is used exactly like JSON, except that Pickle supports packaging complex objects into string form the and JSON can't do that. - " " - #serialization (Serializable) - #list = [1,2,3,4,5,6] + #maps = {"name": "Alex", "Age": - #f = open ("Jsonpractice.text", "W") + #f.write (Json.dumps (list) + "\ n" +json.dumps (MAPS) A #f.close () at - #deserialization - #f1 = open ("Jsonpractice.text", "RB") - #data = Json.loads (F1.read ()) - #print (data) - #For I in Data.keys (): in #print (Data[i]) - #f1.close () to + #Serialization of Pickle - #def goby (): the #print ("Good Bye") * #maps = {"name": "Alex", "Age": $ #maps["func"] = "def goby ():Panax Notoginseng #print (' Good by ') ' - #f = open ("Jsonpractice2.text", "WB") the #F.write (Pickle.dumps (maps)) #pickle. Dump (maps,f) + #f.close () A the #Pickle Deserialization + #f1 = open ("Jsonpractice2.text", "RB") - ##data = Pickle.loads (F1.read ()) $ #data = pickle.load (F1) $ #print (data["func"]) - #exec (data["func"]) - #f1.close () the - Wuyi #character transcoding Implementation the #test = "Print (' Hello ')" - #print (sys.getdefaultencoding ()) Wu #Print (Test.encode ("Utf-8"). Decode ("ASCII", "replace"). Encode ("ASCII", "replace"). Decode ("Utf-8")) - #exec (test)
Python operation JSON to store simple data, pickle to manipulate complex data