Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.

Review and

Python supports both built-in libraries and third-party libraries for serializing and deserializing data

The Python built-in library

  • Sequence and deserialize data using binary protocols -pickle module
  • Convert data to JSON format and deserialize operations – JSON module
  • Sequence and deserialize data using dictionary-like key values -shelve module
  • Parse commands and options in the command window -argparse module

Python Third-party Libraries

  • Third party library, is an upgraded version of json module to optimize useless characters – Messagepack module
  • Yaml configuration file data sequence and anti-sequence operations – Yaml module

In this issue, we will continue our learning and use of the popular yamL module methods. Let’s go~

1. Introduction of yaml

Yaml is a language specifically designed for configuration files or data storage that end in. Yaml

Yaml characteristics

  1. Yaml is easy to read and easy to write
  2. Yaml can be used for descriptive data structures such as scripts
  3. Yaml is suitable for data exchange between different programs
  4. Yaml is highly extensible and is not restricted by platform or language

Yaml Installation method:

pip install pyyaml
Copy the code

Yaml module import:

import yaml
Copy the code

2. Common syntax of YAML

Yaml documents consist of one or more documents, which are distinguished as follows

  • Start each document with:---. If the document is a single document, omit it
  • End each document with:.. optional

Yaml supports three types of data structures

  • Scalar: string, Boolean, integer, Null, time, date
  • Array: list
  • Key table: dictionary

Yaml uses & and * references

name: &name Juejin
user: *name
Copy the code

Yaml use!!!!!Cast type

String: !!!!! str 3.14
Int: !!!!! int "1234"
Copy the code

PS: More official YAML documentation on YAML syntax

3. Common methods of YAML

Yaml, like other serialization modules json and pickle, also has dump and load methods

methods instructions
yaml.dump(obj,fp) Convert Python objects and enter them into a YAML document
yaml.dump_all(obj,fp) Enter multiple Python objects into a YAML document
yaml.load() Convert yamL files into Python objects
yaml.load_all() Convert multiple yamL statements into Python objects

4. yaml Vs json

Yaml and JSON are both data serialization languages.

  • Yamls is not a markup language and has an emphasis on data
  • Json is a JAVASCRIPT object tag, a lightweight data interchange format
{
  "juejiner one": {
    "name": "Tom"."age": 21
  },
  "juejiner two": {
    "name": "Tony"."age": 12}}Copy the code

The above is in JSON format, let’s take a look at yamL conversion effect

# yaml
---
juejiner one:
  name: Tom
  age: 21
juejiner two:
  name: Tony
  age: 12
Copy the code

Yaml and Json usage scenarios

  • Json is better for data returned by network requests
  • Yaml is more suitable for data storage and reading and is often used in automated test imports

PS: We can use online conversion tools when we debug

5. Test the cat

Let’s use Python to parse the previous YAML data

import yaml Juer = { "juejiner one": { "name": "Tom", "age": 21 }, "juejiner two": { "name": "Tony", "age": Yaml_text = yaml. Dump (Juer) print(yaml_text) print("------------"  yaml.load(yaml_text,Loader=yaml.FullLoader) print(py)Copy the code

conclusion

In this installment, we’ll learn about the simple use of yamL module methods

The above is the content of this issue, welcome to the big people like the comments and corrections, see next time ~ღ(´ ᴗ · ‘) than the heart 🌹🌹 ᴗ Stan ღ