1. Yaml file introduction
Yaml is a special language for writing configuration files.
1. Yaml file rules
- Case sensitive;
- Use indentation to indicate hierarchy;
- use
The blank space key
The indentation, andThe Tab key
The indentation - The number of indent Spaces is not fixed, only elements of the same level need to be aligned to the left;
- Strings in the file do not need to be marked with quotation marks. However, if the string contains special characters, it must be marked with quotation marks.
- The comment is identified with #
2. Yaml file data structure
- Object: A collection of key-value pairs (” mapping or dictionary “for short). Key-value pairs are represented by colons (:), separated by Spaces
- Array: An array of ordered values (” sequence or list “for short) preceded by a hyphen (-), separated by a space
- Scalars: Single, non-separable values (strings, bool, integer, floating point, time, date, null, etc.) None can be represented by ~ with null
Read yamL configuration files in Python
1. Prerequisites
To read yaml files in Python, you need to install Pyyaml and import yaml modules:
- To use YAMl, install pyyaml (
pip3 install pyyaml
); - The imported module is YAMl (
import yaml
)
2. Read yamL file data
Python reads file data in open mode and converts the data into a list or dictionary using the load function.
import yaml
import os
def get_yaml_data(yaml_file) :
Open the YAML file
print("*** Get 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 strings to dictionaries or lists
print("*** Convert YAML data to dictionaries or lists ***")
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)
Usr: my PSW: 123455 Type:
*** Convert YAMl data to dictionaries or lists *** {'usr': 'my', 'PSW ': 123455}
Copy the code
3. The yamL file contains key-value pairs
(1) Contents in yamL file are key-value pairs:
# YAMl key-value pair: dictionary in Python
usr: my
psw: 123455
s: " abc\n"
Copy the code
Python retrieves data from yaml files:
{'usr': 'my'.'psw': 123455, 's': ' abc\n'}
Copy the code
(2) YamL file contains contents of “key-value pair ‘nested” key-value pair
# yamL key-value pair nesting: dictionary nesting in Python
usr1:
name: a
psw: 123
usr2:
name: b
psw: 456
Copy the code
Python retrieves data from yaml files:
{'usr1': {'name': 'a'.'psw': 123}.'usr2': {'name': 'b'.'psw': 456}}
Copy the code
(3) “array” nested in “key-value pair” in YAML file
# nested arrays in yamL key-value pairs
usr3:
- a
- b
- c
usr4:
- b
Copy the code
Python retrieves data from yaml files:
{'usr3': ['a'.'b'.'c'].'usr4': ['b']}
Copy the code
4. Yaml file data is an array
(1) YamL file contains an array
# yaml array
- a
- b
- 5
Copy the code
Python retrieves data from yaml files:
['a', 'b', 5]
Copy the code
(2) “key-value pairs” nested in yamL file “Array”
# yaml"Array"The nested"Key value pair"
- usr1: aaa
- psw1: 111
usr2: bbb
psw2: 222
Copy the code
Python retrieves data from yaml files:
[{'usr1': 'aaa'}, {'psw1': 111, 'usr2': 'bbb'.'psw2': 222}]
Copy the code
5. Basic data types in YAML files:
# scalar s_val: name'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: {'bol_val': True}
nul_val: null # nullValue: {'nul_val': None}
nul_val1: ~ # nullValue: {'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)}
Copy the code
6. Reference in the YAML file
Yaml file contents
animal3: &animal3 fish
test: *animal3
Copy the code
Data read by Python
{'animal3': 'fish'.'test': 'fish'}
Copy the code
Read multiple YAML documents in Python
1. Multiple documents in a YAML file are segmented using a — partition method
For example, data in yamL files
Segment multiple documents in a YAML file
---
animal1: dog
age: 2
---
animal2: cat
age: 3
Copy the code
2. The Python script reads multiple document methods in a YAML file
Python retrieves yamL data using the load_all function to parse the entire document and read the data from the object
When a YAML file contains multiple documents, fetch the data from the documents separately
def get_yaml_load_all(yaml_file) :
Open the 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)
"" "results {' animal1 ':' dog ', 'age, 2} {' animal2' : 'the cat', 'age: 3} "" "
Copy the code
Python objects generate YAML documents
1. Directly import yamL documents generated by YAML
The yaml.dump() method does not convert list or dictionary data to yamL standard mode, but only generates the data into a YAML document
# Generate Python objects into YAML documents
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)
Results School: Zhang Students: [A, B] ""
Copy the code
2. Use the YAML method in ruamel module to generate standard YAML documents
(1) Use yamL prerequisites in ruamel module
- Yaml: ruamel.yaml (pip3 install ruamel.yaml);
- Import module: from ruamel import yaml
(2) The RUamel module generates yamL documents
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)
""" Results School: Zhang Students: a-B ""
Copy the code
(3) Ruamel module reads yamL document
# read yaml file 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)
Copy the code