"PyYAML"의 두 판 사이의 차이

 
30번째 줄: 30번째 줄:
o = yaml.safe_load(y)
o = yaml.safe_load(y)
print(type(o)) # <class 'dict'>
print(type(o)) # <class 'dict'>
print(o) # {'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']}
print(o)
</syntaxhighlight>
</syntaxhighlight>
{{소스헤더|dict to YAML}}
{{소스헤더|dict to YAML}}
37번째 줄: 37번째 줄:
d = {'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45,'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}
d = {'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45,'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}
y = yaml.dump(d)
y = yaml.dump(d)
print(type(y))
print(type(y)) # <class 'str'>
print(y)
print(y)
</syntaxhighlight>
</syntaxhighlight>

2022년 1월 8일 (토) 13:02 기준 최신판

1 개요[ | ]

PyYAML
import yaml
with open("example.yaml", 'r') as stream:
    try:
        print(yaml.safe_load(stream))
    except yaml.YAMLError as e:
        print(e)
YAML to dict
import yaml
y = """
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
"""
o = yaml.safe_load(y)
print(type(o)) # <class 'dict'>
print(o)
dict to YAML
import yaml
d = {'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45,'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}
y = yaml.dump(d)
print(type(y)) # <class 'str'>
print(y)

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}