YAML语法规则:
http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/
下载PyYAML:
解压安装: python setup.py install
1.新建test.yaml文件,内容如下:
name: Tom Smith age: 37 spouse: name: Jane Smith age: 25 children: - name: Jimmy Smith age: 15 - name1: Jenny Smith age1: 12
2.创建python文件test.py,内容如下:
#!/usr/bin/python # -*- coding: utf-8 -*- import yaml f = open(‘test.yaml‘) x = yaml.load(f) print x
3.运行python test.py 结果如下:
{‘age‘: 37, ‘spouse‘: {‘age‘: 25, ‘name‘: ‘Jane Smith‘}, ‘name‘: ‘Tom Smith‘, ‘children‘: [{‘age‘: 15, ‘name‘: ‘Jimmy Smith‘}, {‘age1‘: 12, ‘name1‘: ‘Jenny Smith‘}]}
时间: 2024-10-14 20:54:10