Second Brain: Crafted, Curated, Connected, Compounded on 10月02日 21:06
YAML配置文件详解
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

YAML是一种常用于编写配置文件的数据序列化语言。它以简洁明了的语法结构著称,支持多种数据类型如字典、列表等。YAML的核心特点在于其可读性强,通过缩进和键值对形式清晰表达数据层级。Python中可通过yaml模块解析.yaml或.yml文件,将其转换为字典等Python对象,便于程序处理。YAML还支持多行文本、注释等功能,使其在配置管理、数据交换等领域得到广泛应用。

📚 YAML是一种数据序列化语言,主要用于编写配置文件,以简洁明了的语法结构著称,支持字典、列表等多种数据类型,可读性强。

🔍 YAML的核心特点在于其可读性强,通过缩进和键值对形式清晰表达数据层级,使得配置信息易于理解和维护。

⚙️ 在Python中,可以通过yaml模块解析.yaml或.yml文件,将其转换为字典等Python对象,便于程序处理和配置管理。

💡 YAML还支持多行文本、注释等功能,使其在配置管理、数据交换等领域得到广泛应用,成为许多现代软件开发中的标准配置格式。

📈 YAML的广泛应用得益于其灵活的数据表示能力和跨语言的兼容性,使得不同编程语言和平台可以方便地交换和处理配置数据。

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

It stands for Yet Another Markup Language.

# The language of declarative configuration

See Descriptive Configs - YAML.

# Reading with Python

# Reading and parsing a YAML file with Python

Once we have the YAML parser imported, we can load a YAML file and parse it. YAML files usually carry the extension .yaml or .yml. Let’s work with the following example YAML file called config.yaml:

12345
rest:  url: "https://example.org/primenumbers/v1"  port: 8443prime_numbers: [2, 3, 5, 7, 11, 13, 17, 19]

Loading, parsing, and using this configuration file is similar to loading JSON with the Python JSON library. First, we open the file. Next, we parse it with the yaml.safe_load() function. Please note that I changed the output a little to make it more readable for you:

 1 2 3 4 5 6 7 8 910111213
>>> import yaml>>> with open('config.yml', 'r') as file...    prime_service = yaml.safe_load(file)>>> prime_service{'rest':   { 'url': 'https://example.org/primenumbers/v1',    'port': 8443  },  'prime_numbers': [2, 3, 5, 7, 11, 13, 17, 19]}>>> prime_service['rest']['url']https://example.org/primenumbers/v1

The YAML parser returns a regular Python object that best fits the data. In this case, it’s a Python dictionary. This means all the regular dictionary features can be used, like using get() with a default value.

# YAML Multiline

https://yaml-multiline.info/

Comment:

    I like plain text, which has the advantage to include " or ' for SQL’s. And the limitiations are not that bad with : and # :
      Plain flow scalars are picky about the : and # characters. They can be in the string, but : cannot appear before a space or newline, and # cannot appear after a space or newline; doing this will cause a syntax error. If you need to use these characters you are probably better off using one of the quoted styles instead.

# Flow Scalars

# Single-quoted

YAML

12345
example: 'Several lines of text,\n··containing ''single quotes''. Escapes (like \n) don''t do anything.\n··\n··Newlines can be added by leaving a blank line.\n····Leading whitespace on lines is ignored.'\n

Result

Several lines of text, containing ‘single quotes’. Escapes (like \n) don’t do anything.\n
Newlines can be added by leaving a blank line. Leading whitespace on lines is ignored.

# Double-quoted

YAML

1234567
example: "Several lines of text,\n··containing \"double quotes\". Escapes (like \\n) work.\nIn addition,\n··newlines can be esc\\n··aped to prevent them from being converted to a space.\n··\n··Newlines can also be added by leaving a blank line.\n····Leading whitespace on lines is ignored."\n

Result
Several lines of text, containing “double quotes”. Escapes (like \n) work.\n
In addition, newlines can be escaped to prevent them from being converted to a space.\n
Newlines can also be added by leaving a blank line. Leading whitespace on lines is ignored.\n

# Plain

YAML

123456
example: Several lines of text,\n··with some "quotes" of various 'types'.\n··Escapes (like \n) don't do anything.\n··\n··Newlines can be added by leaving a blank line.\n····Additional leading whitespace is ignored.\n

Result

Several lines of text, with some “quotes” of various ’types’. Escapes (like \n) don’t do anything.\n
Newlines can be added by leaving a blank line. Additional leading whitespace is ignored.

Note: Plain flow scalars are picky about the : and # characters. They can be in the string, but : cannot appear before a space or newline, and # cannot appear after a space or newline; doing this will cause a syntax error. If you need to use these characters you are probably better off using one of the quoted styles instead.


References:

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

YAML 配置文件 数据序列化 Python 配置管理
相关文章