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'}
相关推荐
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程2 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪2 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook2 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田2 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python