A few examples of using YAML in Python

Source: Internet
Author: User
Tags python script

Python version: 2.7.5

Installation method: Pip Install Pyyaml


The method of "writing variables into Yaml for configuration files and then fetching variables from a Yaml file from a python script" is recently a popular configuration item method in Python programming. Yaml is easier to read and is indented to represent the structure, which coincides with Python.


Yaml has four more commonly used usages, namely load (), dump (), Load_all (), Dump_all (). This article is mainly about the four methods.


First, let's write a very simple test.py:

#-*-Coding:utf-8-*-#!/usr/bin/env pythonimport yamlyaml_str = "" "Name:gakkiage:29job:actressrelationship:wife" "" AA A = Yaml.load (yaml_str) Print AAA

If you do, you see the effect:

[[email protected] chentest]# python test.py {' job ': ' Actress ', ' age ': ' relationship ': ' Wife ', ' name ': ' Gakki '}


This AAA type is a dictionary (dict), if you want to get inside that "Gakki", then is aaa[' name ']. with the Load method, a string becomes a dictionary.


Now replace the test.py with the following:

#-*-Coding:utf-8-*-#!/usr/bin/env Pythonimport yamlyaml_dict = {"Name": "Gakki", "Age": "Job": "Ac Tress "," relationship ":" Wife "}aaa = Yaml.dump (yaml_dict, default_flow_style=false) print Aaaprint (t Ype (AAA))

The following results are performed:

[[email protected] chentest]# python test.py age:29job:actressname:gakkirelationship:wife<type ' str ' >


As can be seen, a dict is transformed into a string by the dump method .


Now write a config file, if it's called Test.yaml:

-Gakki-29-actress-wife


One more test.py, the following:

#-*-Coding:utf-8-*-#!/usr/bin/env pythonimport yamlaaa = yaml.load (file (' Test.yaml ', ' R ')) print aaaprint (Type (AAA))

Perform this test.py:

[[email protected] chentest]# python test.py [' Gakki ', ' actress ', ' Wife ']<type ' list ' > #得到了一个列表


If you upgrade that Test.yaml to a mixed structure of dictionaries and lists, the following:

-Name:chris age:29 job:om Engineer-name:gakki age:29 job:actress relationship:wife


The effect of performing test.py is as follows:

[[email protected] chentest]# python test.py [{' Job ': ' OM Engineer ', ' age ': ' Name ': ' Chris '}, {' Job ': ' Actress ', ' age ' : ' Relationship ': ' Wife ', ' name ': ' Gakki '}]<type ' list ' >

Now that the result is a list containing a dictionary, if you want to get "Gakki" is aaa[1][' name ']


If you want to copy and reference, then use & and *, for example, change Test.yaml to this:

Name: &name gakkiwife: *name

The effect of performing test.py is as follows:

[[email protected] chentest]# python test.py {' name ': ' Gakki ', ' wife ': ' Gakki '}<type ' dict ' >


in the same Yaml file, you can use   ---   to fragment so that multiple documents can be written in one file:

---name:chris age:29 job:om Engineer---name:gakki age:29 job:actress relationship:wife

In writing a new test.py is as follows:

#-*-Coding:utf-8-*-#!/usr/bin/env pythonimport yamlys = yaml.load_all (file (' Gakki.yaml ', ' R ')) #load_all () method generates an iterative can be output for y in Ys:print y

Perform the effect of this py:

[[email protected] chentest]# python test.py {' job ': ' OM Engineer ', ' age ': ' Name ': ' Chris '} {' job ': ' Actress ', ' Age ': 2 9, ' relationship ': ' Wife ', ' name ': ' Gakki '}


Reference Document: Https://huilansame.github.io/huilansame.github.io/archivers/recommond-case-file-type-yaml

A few examples of using YAML in 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.