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
test_yaml.py 代码如下:
import yaml with open('familyInfo.yaml', 'r' ,encoding='utf-8') as file: data = yaml.load(file, Loader=yaml.FullLoader) print(data)
执行结果如下:
{ '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