吉吉于

free

【Yaml】yaml

官网 http://pyyaml.org/wiki/PyYAML

昨天用了一下yaml,记一下。

YAML是一种直观的能够被电脑识别的的数据序列化格式,容易被人类阅读,并且容易和脚本语言交互。YAML类似于XML,但是语法比XML简单得多,对于转化成数组或可以hash的数据时是很简单有效的。

YAML被很多人认为是可以超越xml和json的文件格式。对比xml,除了拥有xml的众多优点外,它足够简单,易于使用。而对于json,YAML可以写成规范化的配置文件(这我认为是高于json很多的优点,用json写配置文件会让人发疯)。

在YAML里面,结构通过缩进来表示,连续的项目通过减号”-”来表示,map结构里面的key/value对用冒号”:”来分隔。
YAML也有用来描述好几行相同结构的数据的缩写语法,数组用’[]‘包括起来,hash用’{}’来包括。

yaml格式:

name: The Cloak 'Colluin'
rarity: 45
flags: [INT, WIS, SPEED, STEALTH]
weight: 10
cost: 50000
depth: 5


>>> import yaml


>>> print yaml.load(""" ... name: Vorlin Laruknuzum ... sex: Male ... class: Priest ... title: Acolyte ... hp: [32, 71] ... sp: [1, 13] ... gold: 423 ... inventory: ... - a Holy Book of Prayers (Words of Wisdom) ... - an Azure Potion of Cure Light Wounds ... - a Silver Wand of Wonder ... """) {'name': 'Vorlin Laruknuzum', 'gold': 423, 'title': 'Acolyte', 'hp': [32, 71], 'sp': [1, 13], 'sex': 'Male', 'inventory': ['a Holy Book of Prayers (Words of Wisdom)', 'an Azure Potion of Cure Light Wounds', 'a Siver Wand of Wonder'], 'class': 'Priest'} 


>>> print yaml.dump({'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45, ... 'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}) 

 

name: The Cloak 'Colluin'
rarity: 45
flags: [INT, WIS, SPEED, STEALTH]
weight: 10
cost: 50000
depth: 5

转载请注明:于哲的博客 » 【Yaml】yaml