9-Yaml format file

Introduction to Yaml format

 Yaml is a language specially used to write configuration files, very concise and powerful, and more convenient than JSON format. It can be used as the configuration file or use case file of the automated testing framework to
 install yaml: pip install PyYaml

Yaml format syntax

basic type

  • String
  • Plastic surgery
  • Floating point
  • Boolean
  • null
  • time
  • date

basic rules

  • Case Sensitive
  • Use indentation to indicate hierarchical relationships
  • Tabs are not allowed when indenting, only spaces are allowed
  • The number of indentation spaces is not important, as long as the elements of the same level are aligned
  • # Means comment the current line
  • Need to add a space after the colon
  • The format of the outer layer should be consistent

1. Yaml to dictionary

#conf.yaml文件
username: user01
password: 123456

#py文件,读取yaml文件并转换成字典
import yaml
def get_yaml_data(filedir):
    with open(filedir,'r',encoding='UTF-8') as file1:
        res = yaml.load(file1,Loader=yaml.FullLoader)
        print(res)

if __name__ == '__main__':
    get_yaml_data('../configs/conf.yaml')

#执行结果:
{
    
    'username': 'user01', 'password': 123456}

2. Yaml to list

#conf.yaml文件
- 10
-
  - 101
  - 102

#执行结果:
[10, [101, 102]]

3. Yaml list set dictionary

#conf.yaml文件
- 10
-
  username: user01  
  password: 123456

#执行结果:
[10, {
    
    'username': 'user01', 'password': 123456}]

4. Yaml dictionary sets list

#conf.yaml文件
username: user01  
password: 123456
info: 
  - 101
  - 102

#执行结果:
{
    
    'username': 'user01', 'password': 123456, 'info': [101, 102]}

5. Handling of special symbols

  • When the character contains \n \t, you need to add double quotes to keep it as it is, otherwise it becomes \n,\t
  • When you need to keep single quotation marks, use double quotation marks in the outer layer

6. Definition and reference of variables

#conf.yaml文件
name: &user Marry
label1: *user
label2: *user

#执行结果:
{
    
    'name': 'Marry', 'label1': 'Marry', 'label2': 'Marry'}

7. Write multiple yaml together

Use—as a separator

#conf.yaml文件
username: marry
password: 123456
---
- 101
- 102
- 103

import yaml
def get_yamls_data(filedir):
    with open(filedir,'r',encoding='UTF-8') as file1:
        resList = []
        res = yaml.load_all(file1,Loader=yaml.FullLoader)  #读多个yaml使用yaml.load_all
        for one in res:
            resList.append(one)
        print(resList)

if __name__ == '__main__':
    get_yamls_data('../configs/conf.yaml')

8. Inherit other yaml files

#a.yaml
username: marry
password: 123456
info: !include b.yaml  # 继承b.yaml文件数据

#b.yaml
- 101
- 102
- 103
#继承yaml .py文件
import yaml
import os.path
class Loader(yaml.Loader):
    def __init__(self,stream):
        self.root = os.path.split(stream.name)[0]
        super(Loader,self).__init__(stream)
    def include(self,node):
        filename = os.path.join(self.root,self.construct_scalar(node))
        with open(filename,'r') as f:
            return yaml.load(f,Loader)
Loader.add_constructor('!include',Loader.include)

if __name__ == '__main__':
    with open('../configs/a.yaml') as f:
        data = yaml.load(f,Loader)
        print(data)

#执行结果:
{
    
    'username': 'marry', 'password': 123456, 'info': [101, 102, 103]}

Guess you like

Origin blog.csdn.net/weixin_45128456/article/details/113247344