創(chuàng)建文件: dev.yaml
languages:
- python
- C++
- golang
zhiyuan:
name: zhiyuan
age: 25
height: 179.50
gender: 男
hobby: &hobby # 這是一個錨點
- 編程
- 爬山
- 音樂
love: False # bool
wife: ~ # null
xiaohong:
name: xiaohogn
age: 22
height: !!str 165.00 # 強轉為字符串
gender: 女
today: 2020-11-28 # datetime.date
time: 2020-11-28t23:23:23+8 # datetime.datetime
haha:
*hobby
readme: 這是一個測試yaml
>>> import yaml
>>> with open('dev.yaml', 'r', encoding='utf-8') as f:
conf = yaml.load(f, Loader=yaml.FullLoader)
>>> conf
{'haha': ['編程', '爬山', '音樂'],
'languages': ['python', 'C++', 'golang'],
'readme': '這是一個測試yaml',
'xiaohong': {'age': 22,
'gender': '女',
'height': '165.00',
'name': 'xiaohogn',
'time': datetime.datetime(2020, 11, 28, 23, 23, 23, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800))),
'today': datetime.date(2020, 11, 28)},
'zhiyuan': {'age': 25,
'gender': '男',
'height': 179.5,
'hobby': ['編程', '爬山', '音樂'],
'love': False,
'name': 'zhiyuan',
'wife': None}}