第三方模块 - yaml

Jogn.yaml

 1 name: John Smith
 2 age: 37
 3 spouse:
 4     name: Jane Smith
 5     age: 25
 6 children:
 7     -   name: Jimmy Smith
 8         age: 15
 9     -   name: Jeny Smith
10         age: 12

test_yaml.py

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import yaml
 4 
 5 path = os.path.join(os.path.dirname(__file__), 'Jogn.yaml')
 6 
 7 with open(path) as f:
 8     data = yaml.load(f)
 9 
10 print(data)

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

注:

     安装:pip3  install  pyyaml

     参考:https://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/index.html

posted @ 2018-03-05 20:11  Alice的小屋  阅读(302)  评论(0编辑  收藏  举报