config.yaml文件读入后是一个字典,可用来配置程序中的相关参数;
config_test.yaml文件
'''
yaml文件 注意事项:
不要用Tab,用空格!用空格!用空格!
'''
name: Tom Smith
age: 37
spouse:
name: Jane Smith
age: 25
children:
- name: Jimmy Smith
age: 15
- name1: Jenny Smith
age1: 12
#config_test.py
import yaml
file_path = r"E:\pythonwork\helloword\config_test.yaml"
f = open(file_path)
config = yaml.load(f)
print type(config)
print config
print config['age']
print config['children']
print config['children'][0]['age']
运行结果:

YAML配置解析
本文介绍了一个使用Python读取和解析YAML配置文件的例子。重点展示了如何加载YAML文件并访问其内部的数据结构,如字典和列表。通过具体代码演示了如何获取文件中的基本信息,例如人物的名字、年龄等。
1749





