Recently saw a lot of use of Yaml file as a configuration file or data file project, then also studied, found that Yaml has several advantages: good readability, and scripting language interactivity (really very good), the use of the implementation of the language data type, have a consistent data model, easy to implement. Yaml is a good thing, whether it's an interface test or UI Automation test, which is often used to store test data. Let's take a look at how to install and use this YAML library.
YAML Installation
1. Download link: Yaml Download portal
2. Select the download version as shown in:
3. Switch to the extract directory below to execute the command: Python3 setup.py install as shown:
4. Enter Python interactive mode and run the following code:
Run without error, indicating that the installation was successful!
Yaml Simple Example
Now create a Yaml file locally, containing data, operations:
The instance code is as follows:
Import= open ('e:\\test.yaml','r') Print (Yaml.load (f))
Output Result:
{' Age': 37,'Spouse': {' Age': 25,'name':'Jane Smith'},'Children': [{' Age': 15,'name':'Jimmy Smith'}, {'name1':'Jenny Smith','Age1': 12}],'name':'Tom Smith'}
Get full file path:
ImportYaml,os#get file Full pathfilename = Os.path.join (Os.path.dirname (__file__),'Test.yaml'). Replace ("\\","/")#filename = os.path.join (Os.path.dirname (__file__), ' Test.yaml ')#print (filename)f =open (filename) y=yaml.load (f)Print(y)
The output results are as follows:
{'name':'Tom Smith','Children': [{'name':'Jimmy Smith',' Age': 15}, {'Age1': 12,'name1':'Jenny Smith'}],'Spouse': {'name':'Jane Smith',' Age': 25},' Age': 37}
Reference Document: Http://www.ruanyifeng.com/blog/2016/07/yaml.html
Manipulate the YAML library using Python