LoginSignup
0
1

More than 5 years have passed since last update.

めざせpythonライブラリマスター (20)pyyaml

Posted at

【ライブラリ説明】

 yamlファイルを整形できます。

【プログラム】

pyyaml.py
# -*- coding: utf-8 -*-

import yaml

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

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

stream = open('document.yaml', 'r')
yaml_data2 = yaml.load(stream)
print (yaml.dump((yaml_data2)))
'''
Hello World: {null: This is madness, gh: 'https://github.com/{0}.git'}
'Yay #python': Cool!
default_context: {greeting: hello, gui: false, someint: 1000000, docs: true, email: raphael@hackebrot.de,
  123: 456.789, foo: 'hallo #welt'}
zZz: true
'''
document.yaml
default_context: # foobar
    greeting: hello
    email: "raphael@hackebrot.de"
    docs: true
    gui: FALSE
    123: 456.789
    someint: 1000000
    foo: "hallo #welt" #Inline comment :)
zZz: True


# Block
# Comment

Hello World:
    null: This is madness   # yo
    gh: https://github.com/{0}.git
"Yay #python": Cool!

【参考サイト】

 PyYAMLホームページ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1