Introduction to JSON and pickle usage

Source: Internet
Author: User
JSON

JSON is one of the modules for serializing and deserializing program data types. It can be used for different platforms, data exchange under different programs, or a method of temporarily preserving data. Let's take a look at JSON usage:

1 #json_序列化. PY 2 Import JSON 3 dic={4     "id": "123456", 5     "name": "Jack", 6     "country": "China" 7     } 8 Date=json . Dumps (DIC) 9 with open ("Demo.txt", "W") as F:10     f.write (date) one #json_反序列化. PY13 Import json14 with open ("Demo.txt" , "R") as F:15     dic1=json.loads (F.read ()) print (dic1["name"])

The contents of the two files are shown above, the first one is to serialize a dictionary into a string (using the Dumps () method) and then write a file (demo.txt). The second file reads the contents of the demo.txt and then deserializes it with the loads () method, and prints out the contents of the dictionary's "name".

In fact, in addition to the dumps () and loads () two methods, JSON has two more simple methods: Dump () and load (). The following shows the usage:

1 #json_序列化2. PY 2 Import JSON 3 dic={4     "id": "123456", 5     "name": "Jack", 6     "country": "China" 7     } 8 with Ope N ("Demo.txt", "W") as F:9     date=json.dump (dic,f)     #json_反序列化2. PY14 Import json15 with open ("Demo.txt", " R ") as F:16     dic1=json.load (f)-Print (dic1[" name "])

As you can see, dump () and load () are the encapsulation of dumps () and loads () and the read and write operations of the file.

Pickle

Pickle also has the above four methods of JSON, and the usage is exactly the same, here I do not do a demonstration. But Pickle's features are even more powerful. JSON can only serialize some relatively simple data objects, such as lists, dictionaries, and so on. Pickle can also serialize complex objects such as functions, classes, and so on.

Here's a demonstration of how pickle serializes and deserializes a function.

1 #pickle_序列化. py 2 Import Pickle 3 #定义函数hello 4 def Hello (name): 5     print ("Hello", name) 6 #定义列表, save hello in 7 dic1={8
   "name": "Mark", 9     "func": Hello10     }11 with open ("Demo.txt", "WB") as F:12     pickle.dump (dic1,f) 13 14 # Pickle_ deserialization. Py15 Import pickle16 ###################### #17 def hello (name):     print ("Hello", name) 19 ############ ########## #20 with open ("Demo.txt", "RB") as f:21     dic2=pickle.load (f) dic2["Func"] ("Jack")

The above also shows the contents of the two files noted. Be aware of the following issues:

1. When it comes to serialization and deserialization of functions, classes, etc., open the file in binary form, notice the "WB" in the display, "RB"

2. If the deserialized function is to be executed in a new file, the source code of the previously defined function must be copied into the new file (note that the Pickle_ is deserialized. The contents of the two rows of # lines in the PY), otherwise it will not work, if there is no content in the # number, The program will error: Attributeerror:can ' t get attribute ' hello ' on <module ' __main__ ' from ' d:/users/lenovo/pycharmprojects/ Myfirstflask/about_json. py ' >

3. You can make changes to the source code of the copied function, just to ensure that the function name is consistent with the function name used in serialization, and if the function is called after the change, it will be executed according to the changed function (of course, this change must be necessary)

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.