二、安装yaml
pip命令: pip install PyYaml
引入:import yaml
用python读取yaml文件如下:
代码:
import yaml
代码如下:
import yaml,os
# 打开yaml文件
caps_dir = 'C:\\Users\\Administrator\\Desktop'
fs = open(os.path.join(caps_dir, "yaml.yaml"),mode='r',encoding="UTF-8")
datas = yaml.load(fs,Loader=yaml.FullLoader) #添加后就不警告了
print(datas["person"]["university"][0]["weight"])
准备的yaml文件如下:
%ymal 1.2
---
person:
name: kmor
age: 22
sex: 男
funny:
- a: football
- b: basktball
address:
provence: 湖北,
city: 武汉,
area: 江夏,
street: 马房山街道办
university:
- A: grilA
weight: 98
height: 164
age: 22
- B: grilB
weight: 96
height: 162
age: 21
subject:
main:electric
sub:law
comment1:
aaaaaaa
bbbbbbb
ccccccc
comment2: >
ddddddd
eeeeeee
fffffff
comment3: |
ggggggg
hhhhhhh
iiiiiii
mother: &mother_info
age: 55
name: jan
woman: *mother_info