基础语法:
一个常见的yaml文件示例:
# 当前yaml文档使用到注释、变量、类对象
ROOT_DIR: &ROOT_DIR /home/xk/WorkSpace/ProjectPath
PATH: *ROOT_DIR
text: 你好
teacher:
- name: Tom
- age: 18
- school: &school THU
college:
- name: College of Computer Science
- school: *school
一个python调用yaml文件示例:
import yaml
# 读取 YAML 文件
with open("xk.yaml", "r") as f:
content = f.read()
# 将 YAML 文件转换为 Python 字典
data = yaml.safe_load(content)
for item in data:
print(item, data.get(item))
''' 输出:
ROOT_DIR /home/xk/WorkSpace/ProjectPath
PATH /home/xk/WorkSpace/ProjectPath
text 你好
teacher [{'name': 'Tom'}, {'age': 18}, {'school': 'THU'}]
college [{'name': 'College of Computer Science'}, {'school': 'THU'}]
'''
使用yaml文件做配置是团队开发的基础素养,理应常用