Manipulate the YAML library using Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.