Detailed explanation of the use of yaml configuration file module in python

不言
Release: 2018-05-02 14:31:51
Original
3148 people have browsed it

This article mainly introduces the detailed explanation of the use of the yaml configuration file module in python. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Like GNU, YAML is a name for saying "no" recursively. The difference is that GNU says no to UNIX, and YAML says no to XML.
YAML is not XML.

Why not XML? Because:

  1. YAML has good readability.

  2. YAML has good interactivity with scripting languages.

  3. YAML uses data types that implement the language.

  4. YAML has a consistent information model.

  5. YAML is easy to implement.

The above 5 items are the shortcomings of XML. At the same time, YAML also has the following advantages of XML:

YAML can be processed based on streams;

YAML has strong expressive capabilities and good scalability.

In short, YAML attempts to use a more agile way than XML to complete the tasks completed by XML.

Syntax

Structures are shown through space indentation. Items in the list are represented by "-", and key-value pairs in the dictionary are separated by ":".

This is almost all the syntax.

For example ……

Generally, the YAML file extension is .yaml. For example: yaml_example.yaml

Write yaml into the configuration script test.yaml. The following describes how to read and write yaml configuration.

Get started

1. First install the yaml module

pip3 install pyyaml
Copy after login

2. Write the yaml configuration file yaml_example.yaml

name: junxi
age: 18
spouse:
 name: Rui
 age: 18
children:
 - name: Chen You
  age: 3
 - name: Ruo Xi
  age: 2
Copy after login

3. Write the python program yaml_example.py that parses the yaml file

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'

import sys

# sys.path.insert(0, 'D:/program/python-腾讯课程/0-01-python其他模块学习/')

import yaml

f = open('yaml_example.yaml')
content = yaml.load(f)

print type(content)
print '修改前: ', content # 可以看出整个Yaml配置文件是一个字典, 里面可以包含字典和列表
content['age'] = 17  # 根据Key修改对应的值
content['children'][1]['age'] = 1
print '修改后: ', content
Copy after login

The result output by the program is:


Before modification : {'age': 18, 'spouse': {'age': 18, 'name': 'Rui'}, 'name': 'junxi', 'children': [{'age': 3, 'name ': 'Chen You'}, {'age': 2, 'name': 'Ruo Xi'}]}
After modification: {'age': 17, 'spouse': {'age': 18, 'name': 'Rui'}, 'name': 'junxi', 'children': [{'age': 3, 'name': 'Chen You'}, {'age': 1, 'name': 'Ruo Xi'}]}

Related recommendations:

Using the seek() method to operate files in Python


The above is the detailed content of Detailed explanation of the use of yaml configuration file module in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!