011、yaml使用初步

 

一、yaml 的使用初步

 

   familyInfo.yaml  示例代码如下:

  注意 spouse—>name: ,age:  冒号后面有空格,如果没有空格会变成字符串,而不是字典嵌套。

name: Tom Smith
age: 37
spouse:
  name: Jane Smith
  age: 25
children:
  - name: Jimmy Smith
    age: 15
  - name: Jenny Smith
    age: 12
View Code

  test_yaml.py  代码如下:

import yaml

with open('familyInfo.yaml', 'r' ,encoding='utf-8') as file:
    data = yaml.load(file, Loader=yaml.FullLoader)
    print(data)
View Code

  执行结果如下:

{
'name': 'Tom Smith', 
'age': 37,
 'spouse': {'name': 'Jane Smith', 'age': 25}, 
'children': 
             [{'name': 'Jimmy Smith', 'age': 15},
              {'name': 'Jenny Smith', 'age': 12}
             ]
}

Process finished with exit code 0
 
View Code

 

posted @ 2021-08-03 19:29  空-山-新-雨  阅读(62)  评论(0编辑  收藏  举报