angela 发表于 2017-5-4 08:35:11

如何用python将yaml格式文件转换为json

如何用python将yaml格式文件转换为json

1 从http://www.pyyaml.org/wiki/PyYAML 下载PyYAML-3.11.zip包

2 解压执行 python setup.py install

3 测试
>>> import yaml

>>> print yaml.load("""
... name: Vorlin Laruknuzum
... sex: Male
... class: Priest
... title: Acolyte
... hp:
... sp:
... 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': ,
'sp': , '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:
weight: 10
cost: 50000
depth: 5
页: [1]
查看完整版本: 如何用python将yaml格式文件转换为json