--python parsing yaml type file with Python base

Source: Internet
Author: User
Tags time and date






Directory






[TOC]




One, Yaml introduction



YAML full name yet another Markup Language (another markup language). Using YAML as a configuration file, the file looks intuitive, concise, and easy to understand. Yaml files can parse the data structures of dictionaries , lists , and some basic variables .




1.1 Yaml Syntax rules

Case sensitive

Using indents to represent hierarchical relationships

TAB key is not allowed when indenting, only spaces can be used

The number of indent spaces is not important as long as the same element is left to it

# indicates when line comment


1.2 Yaml Environment Building

-After installing pip, do the following

pip install pyyaml

Second, the yaml file format 2.1 Dictionary

# General dictionary

key1: value


# Nested dictionary

key2:

   sub_key1: value1

   sub_keys: value2

2.2 List

# Below the same level of para1, para2 and para3 in the same list

-para1

-para2

-para3

2.3 Ordinary variables

       The yaml configuration file can parse numbers, strings, Boolean data, time and date formats, and can also perform forced conversions on numbers and Boolean data to parse it into string data


2.3.1 Representation of None in yaml

# In yaml ~ means None

~

2.3.2 yaml cast data type

# In the yaml configuration, !! str data means to cast the data data to str

age: !! str 18

2.3.3 yaml date format representation

# Both time and date formats are iso8601


# Date indication

data_today: 2018-04-22


# Time format

# The following represents Beijing time 2018, 04, 22 at 16:55:30, because Beijing is located in the East Eight District, so 08:00 is added at the back, and the seconds can be written to two decimal places

time_now: 2018-04-22T16: 55: 30 + 08: 00

Three, yaml file reading

Import yaml module, need to use the official import method, compatible with windows and linux platforms


import yaml

try:

   from yaml import CLoader as Loader, CDumper as Dumper

except ImportError:

   from yaml import Loader, Dumper

yaml_file = open ("path", "r")

data = yaml.load (yaml_file)

Fourth, use case 4.1 yaml file to be operated

# File name test.yaml

bind1:

 hostname: ubuntu test

 remote_users:

   -user1:

     username: root

     auth_type: ssh-key

     password: 123

   -user2:

     username: gungun

     auth_type: ssh-password

     password: gungun123

 groups:

   -bj_group

 user_profiles:

   -gungun

   -xiangqiangun

4.2 yaml file reading example

import yaml

try:

   from yaml import CLoader as Loader, CDumper as Dumper

except ImportError:

   from yaml import Loader, Dumper


yaml_file = open ("test.yaml", ‘r’)

data = yaml.load (yaml_file)

print ("data_type:", type (data))

print ("data_content: \ n", data)

Print results:


data_content:

{'bind1': {'user_profiles': [' gungun ',' xiangqiangun '],' hostname ':' ubuntu test ',' groups': ['bj_group'], 'remote_users': [{' username ':' root ',' auth_type ':' ssh-key ',' user1 ': None,' password ': 123}, {' username ':' gungun ',' auth_type ':' ssh-password ',' user2 ': None , 'password': 'gungun123'}]}}

01 Python basics-Python parses yaml type files



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.