Python operation JSON to store simple data, pickle to manipulate complex data

Source: Internet
Author: User
Tags string format

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.