python configuration yaml

  We both interfaces automation or UI Automation will exist a lot of data to make automated process, we have to automate how to store these data is also important that, if written in code words, each data replacement is a bit complicated, we can when a file to store the data, then we need to call this data directly for the management of these data, there are many such as: excel, yaml, ini, conf, today to introduce a yaml.

yaml

yaml (yaml not Markup Language) is a data sequence of the human-readable language. It is commonly used profile, but is also used to store data (e.g., debug output) or transmitted (e.g., title of the document).

yaml supported types : scalar (e.g., string, integer and floating point), and a list of associated arrays.

yaml basic syntax :

1, case sensitive;

2, using indention hierarchical relationships, can only use spaces;

3, list items by "-" indicates, by the dictionary ":" means

4, # indicates a comment, and express comments, like python

yaml advantages:

1, yaml readability is good;

2, interactivity and scripting language yaml good;

3, yaml implement a data type of language used;

4, yaml a consistent information model;

5, yaml easy to implement;

6, yaml may be processed based on the flow;

7, yaml strong communication skills and good scalability;

installation

pip install pyyaml

Creating yaml

Quiet here using pycharm, create a file, just in front of the file name, file extension back .yaml

Write yaml

We can clearly read through this yaml out, called quiet, aged 18, is a professional software test engineer, salary 3K, will have an interface to test the skills of the value 1K, 2K will function test values

# Profile 
name: AnJing
Age: 18
Profession:
name: Software Test Engineer
Annual: 3K
skill:
- NAME1: Interface test
the Pay: 1K

- NAME2: functional testing
pay: 2K

Read yaml

# Coding: UTF. 8- 
Import YAML
 # open directly read out 
F = Open (R & lt ' E: \ App \ Interface \ name.yaml ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) 
Result = reached, f.read ()
 Print (Result)
 # is converted into read out dictionary 
A = yaml.load (Result)
 Print (A)

This time we found that the data is successfully read out, but will certainly be raped, why incorrect report? Where is the problem?

Suppress warning

We can see that this place is prohibited yaml.load (), because the python is considered unsafe load, issued a security warning, then how do we cancel the warning?

yaml.load(result, Loader=yaml.FullLoader)

 Other wording of yaml

# 1, literal values are expressed in the form of direct 
Number: 12.30 # 2, represented by the Boolean true and to false isSet: true 
isSet1: to false # . 3, ~ represents a null 
parent: ~ # . 4, the time in ISO8601 format. 
TIME1: 2019-11-1 21: 59: 43.10-05: 00 # # 5, the date composite iso8601 format year, month, day. 
DATE: 2019-11-1 # . 6, allowing the use of two YAML exclamation, cast data types. 
int_to_str: !! STR 123 
BOOL_TO_STR: STR to true !!










 

Specific values ​​read yaml

Yaml first create a file, the test data is written, read out in the form of a dictionary, the extract according to the dictionary format

# Profile 
name: AnJing 
Age: 18 
Profession: 
      name: Software Test Engineer 
      pay: 2K 
skill:
       - NAME1: Interface test 
        the Pay: 1K

       - NAME2: functional testing 
        pay: 2K

According to the method, value is found by way of the dictionary, and then taken out

# Coding: UTF. 8- 
Import YAML
 # open directly read out 
F = Open (R & lt ' E: \ App \ Interface \ name.yaml ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) 
Result = reached, f.read ()
 # converted to a dictionary to read out the 
a = yaml.load (the Result, Loader = yaml.FullLoader)
 Print (a)
 Print ( ' my name is:% S ' % a [ ' name ' ])
 Print ( ' I do job is: S% ' % A [ ' Profession '] [ ' Name ' ])
 Print ( ' I am% s years old ' % A [ ' Age ' ])
 Print ( ' My salary is% s ' % A [ ' Profession ' ] [ ' the Pay ' ])
 Print ( ' {I} and skills are {} ' .format (a [ ' skill ' ] [0] [ ' NAME1 ' ], a [ ' skill ' ] [. 1] [ ' NAME2 ' ]))



-------------------------------------------------- ----------------------- 
{ ' name ' : ' AnJing ' , ' Age ' : 18 is, ' Profession ' : { ' name ' : ' software testing engineer ' , ' Pay ' : ' 2K ' }, ' skill ' : [{ ' NAME1 ' : ' interface test ' , ' Pay ': '1K ' }, { ' NAME2 ' : ' functional test ' , ' the Pay ' : ' 2K ' }]} 
My name is: AnJing 
I do post are: software test engineer 
I am 18 years old 
my salary is 2K 
I will the skills are the interface and functional testing

 

 

 

 

Guess you like

Origin www.cnblogs.com/qican/p/11773967.html