Json and pickle, jsonpickle

Source: Internet
Author: User

Json and pickle, jsonpickle

Json

Json is one of the modules for serialization and deserialization of program data types. It can be used to exchange data between different platforms and programs, or to temporarily store data in programs. Let's take a look at the usage of json:

1 # json _ serialization. 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) 11 12 # json _ deserialization. py13 import json14 with open ("demo.txt", "r") as f: 15 dic1 = json. loads (f. read () 16 print (dic1 ["name"])

). The second file is to read the content in demo.txt, then use the loads () method to deserialize the dictionary object that can be executed, and print the dictionary "name" content.

In addition to dumps () and loads (), json also has two simpler methods: dump () and load (). The usage is as follows:

1 # json _ serialization 2.py 2 import json 3 dic = {4 "id": "123456", 5 "name": "Jack", 6 "country ": "China" 7} 8 with open ("demo.txt", "w") as f: 9 date = json. dump (dic, f) 10 11 12 13 # json _ deserialization 2. py14 import json15 with open ("demo.txt", "r") as f: 16 dic1 = json. load (f) 17 print (dic1 ["name"])

By comparison, we can see that dump () and load () encapsulate dumps (), loads (), and file read/write operations.

 

Pickle

Pickle also has the preceding four methods of json, and the usage is the same. I will not describe it here. However, pickle is more powerful. Json can only serialize some simple data objects, such as lists and dictionaries. Pickle can also serialize functions, classes, and other complex objects.

The following shows how to serialize and deserialize a function in pickle.

1 # pickle _ serialization. py 2 import pickle 3 # definition function hello 4 def hello (name): 5 print ("hello", name) 6 # definition list, save hello to 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): 18 print ("hello ", name) 19 ###################### 20 with open ("demo.txt", "rb ") as f: 21 dic2 = pickle. load (f) 22 dic2 ["func"] ("Jack ")

The above also shows the content of the two files. Pay attention to the following issues:

1. When serialization and deserialization of functions and classes are involved, open the file in binary format when opening the file. Note that "wb" and "rb" are displayed"

2. if you want to execute the deserialization function in a new file, you must copy the source code of the previously defined function to the new file (note the pickle _ deserialization. in The py column), otherwise it will not run. If there is no content in the #, the program will report an 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. You only need to ensure that the number of function numbers is consistent with the name of the function used for serialization. If you call this function after the change, then execute according to the changed function (of course, this change must be necessary)

 

Author: Peng qianchao

Time:

 

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.