yaml 入门教程

文章目录

https://www.ruanyifeng.com/blog/2016/07/yaml.html

python 读yaml文件

先安装 pip install pyyaml

读取

python 复制代码
import yaml

with open("config.yaml", "r", encoding="utf-8") as f:
    data = yaml.safe_load(f)

print(data)          # 读出来是 dict / list 组合

对象

看到 : → 这是"字典"的一项

数组

看到 - → 这是"列表"的一个元素

复合结构

python 复制代码
{'languages': ['Ruby', 'Perl', 'Python'], 'websites': {'Perl': 'use.perl.org', 'Python': 'python.org', 'Ruby': 'ruby-lang.org', 'YAML': 'yaml.org'}}

纯量

python 复制代码
{'isSet': True, 'number': 12.3, 'parent': None}


字符串

单引号,双引号

单引号 '...':基本"原样",不解析 \n、\t 这些转义序列(它们就当普通字符)。

例:'内容\n字符串' 里面的 \n 就是两个字符:反斜杠和 n。

双引号 "...":会解析转义序列。特殊字符会输出为其本身想表达的含义

例:"内容\n字符串" 里的 \n 会变成真正的换行

python 复制代码
{'s1': '内容\\n字符串', 's2': '内容\n字符串'}
python 复制代码
{'str': "labor's day", 'str1': "labor''s"}
python 复制代码
{'this': 'Foo\nBar\n', 'that': 'Foo Bar'}
Foo
Bar

Foo Bar
yaml 复制代码
str: 这是一段
  多行
  字符串

this: |
  Foo
  Bar
that: >
  Foo
  Bar

s1: |
  Foo

s2: |+
  Foo


s3: |-
  Foo


s4: |-
  Foo
{'s1': 'Foo\n', 's2': 'Foo\n\n\n', 's3': 'Foo', 's4': 'Foo', 'str': '这是一段 多行 字符串', 'that': 'Foo Bar\n', 'this': 'Foo\nBar\n'}
相关推荐
wj3055853781 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李1 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154152 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君5 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG6 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780516 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL7 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆7 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
Miss_min8 小时前
128K长序列数据生成
开发语言·python·深度学习