Catalog
2. python reads yaml configuration file
3. yaml file data is a key-value pair
5. Basic data types in yaml files
3. python reads multiple yaml documents
1. Multiple documents in one yaml file, segmented by--separating
2. python scripts read multiple document methods in a yaml file
4. python object generation yaml document
1. Import yaml documents generated by yaml (that is, import yaml)
2. Use the yaml method in the ruamel module to generate standard yaml documents
1. Introduction of yaml file
yaml is a language dedicated to writing configuration files.
1. yaml File Rules
- Case sensitive;
- Use indentation to represent hierarchical relationships;
- Use space key indentation instead of Tab key indentation
- The number of indented spaces is not fixed, only the left side alignment of elements of the same level is required.
- Strings in files do not need to be labeled with quotation marks, but if they contain special characters, they need to be labeled with quotation marks.
- Comments are identified as #
2. yaml file data structure
- Object: A collection of key-value pairs (referred to as a map or dictionary)
Key-value pairs are represented by a colon':'structure with spaces separating the colon from the value - Array: A set of ordered values (referred to as a sequence or list)
Array is preceded by a'-'symbol, separated by spaces - Scalars: a single, non-divisible value (such as string, bool value, integer, floating point number, time, date, null, etc.)
None values can be expressed as null s
2. python reads yaml configuration file
1. Prerequisites
python needs to install pyyaml and import yaml modules before reading yaml files:
- The module you need to install to use yaml is pyyaml (pip3 install pyyaml);
- The module imported is yaml (import yaml)
2. Read yaml file data
python reads file data open ly and converts it into a list or dictionary through the load function.
import yaml import os def get_yaml_data(yaml_file): # Open yaml file print("***Obtain yaml file data***") file = open(yaml_file, 'r', encoding="utf-8") file_data = file.read() file.close() print(file_data) print("Type:", type(file_data)) # Convert a string to a dictionary or list print("***conversion yaml Data is a dictionary or list***") data = yaml.load(file_data) print(data) print("Type:", type(data)) return data current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "config.yaml") get_yaml_data(yaml_path) """ ***Obtain yaml file data*** # yaml key-value pair: a dictionary in python usr: my psw: 123455 //Type: <class'str'> ***conversion yaml Data is a dictionary or list*** {'usr': 'my', 'psw': 123455} //Type: <class'dict'> """
3. yaml file data is a key-value pair
(1) The contents of the yaml file are key-value pairs:
# yaml key-value pair: a dictionary in python usr: my psw: 123455 s: " abc\n"
The data python gets after parsing the yaml file:
{'usr': 'my', 'psw': 123455, 's': ' abc\n'}
(2) yaml file contains "key-value pairs" nested "key-value pairs"
# yaml key-value pair nesting: a dictionary nested dictionary in python usr1: name: a psw: 123 usr2: name: b psw: 456
The data python gets after parsing the yaml file:
{'usr1': {'name': 'a', 'psw': 123}, 'usr2': {'name': 'b', 'psw': 456}}
(3) Nested Arrays in Key-Value Pairs in yaml Files
# A nested array of yaml key-value pairs usr3: - a - b - c usr4: - b
The data python gets after parsing the yaml file:
{'usr3': ['a', 'b', 'c'], 'usr4': ['b']}
4. yaml file data is an array
(1) The contents of the yaml file are arrays
# yaml array - a - b - 5
The data python gets after parsing the yaml file:
['a', 'b', 5]
(2) Nested Key-Value Pairs in Array of yaml File
# Nested Key-Value Pairs in yaml Array - usr1: aaa - psw1: 111 usr2: bbb psw2: 222
The data python gets after parsing the yaml file:
[{'usr1': 'aaa'}, {'psw1': 111, 'usr2': 'bbb', 'psw2': 222}]
5. Basic data types in yaml files
# Pure Quantity s_val: name # String: {'s_val':'name'} spec_s_val: "name\n" # Special string: {'spec_s_val':'name\n' num_val: 31.14 # Number: {'num_val': 31.14} bol_val: true # Boolean value: {'bol_val': True} nul_val: null # null value: {'nul_val': None} nul_val1: ~ # null value: {'nul_val1': None} time_val: 2018-03-01t11:33:22.55-06:00 # Time value: {'time_val': datetime.datetime(2018, 3, 1, 17, 33, 22, 550000)} date_val: 2019-01-10 # Date value: {'date_val': datetime.date(2019, 1, 10)}
6. References in yaml files
Contents in yaml file
animal3: &animal3 fish test: *animal3
python read data
{'animal3': 'fish', 'test': 'fish'}
3. python reads multiple yaml documents
1. Multiple documents in one yaml file, segmented by--separating
For example: data in yaml file
# Multiple documents in segmented yaml file --- animal1: dog age: 2 --- animal2: cat age: 3
2. python scripts read multiple document methods in a yaml file
python uses the load_all function to parse the entire document and read the data from the object when obtaining yaml data
# When a yaml file contains more than one document, retrieve the data in the document separately def get_yaml_load_all(yaml_file): # Open yaml file file = open(yaml_file, 'r', encoding="utf-8") file_data = file.read() file.close() all_data = yaml.load_all(file_data) for data in all_data: print(data) current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "config.yaml") get_yaml_load_all(yaml_path) """Result {'animal1': 'dog', 'age': 2} {'animal2': 'cat', 'age': 3} """
4. python object generation yaml document
1. Import yaml documents generated by yaml (that is, import yaml)
Using the yaml.dump() method does not transform list or dictionary data into the yaml standard schema; it only generates data into the yaml document
# Generating a yaml document from a python object import yaml def generate_yaml_doc(yaml_file): py_object = {'school': 'zhang', 'students': ['a', 'b']} file = open(yaml_file, 'w', encoding='utf-8') yaml.dump(py_object, file) file.close() current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "generate.yaml") generate_yaml_doc(yaml_path) """Result school: zhang students: [a, b] """
2. Use the yaml method in the ruamel module to generate standard yaml documents
(1) Using yaml preconditions in the ruamel module
- Modules that need to be installed to use yaml: ruamel.yaml (pip3 install ruamel.yaml);
- Imported module: from ruamel import yaml
(2) ruamel module generates yaml document
def generate_yaml_doc_ruamel(yaml_file): from ruamel import yaml py_object = {'school': 'zhang', 'students': ['a', 'b']} file = open(yaml_file, 'w', encoding='utf-8') yaml.dump(py_object, file, Dumper=yaml.RoundTripDumper) file.close() current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "generate.yaml") generate_yaml_doc_ruamel(yaml_path) """Result school: zhang students: - a - b """
(3) ruamel module reads yaml document
# Read yaml files from ruamel import yaml def get_yaml_data_ruamel(yaml_file): from ruamel import yaml file = open(yaml_file, 'r', encoding='utf-8') data = yaml.load(file.read(), Loader=yaml.Loader) file.close() print(data) current_path = os.path.abspath(".") yaml_path = os.path.join(current_path, "dict_config.yaml") get_yaml_data_ruamel(yaml_path)