(public)
/docs/Tips/ML  /docs/Tips  /docs  
YAML

プログラマーのための YAML 入門 (初級編)
http://magazine.rubyist.net/?0009-YAML

言語別 YAML用ライブラリ徹底解説
第3回 Python編
http://gihyo.jp/dev/serial/01/yaml_library/0003


http://yaml.org/

$ cat test00.py
import yaml
import json

docs=[]

# Sequence 配列
# スペースとダッシュ(-)
document = """
- Ginyu
- Recumu
- chize
"""
docs.append(document)

# Mapping Hash 辞書
#  キーと値の対応   キー : 値
document = """
  a: 1
  b:
    c: 3
    d: 4
"""
docs.append(document)


document = """
american:
  - Boston Red Sox
  - Detroit Tigers
  - New York Yankees
national:
  - New York Mets
  - Chicago Cubs
  - Atlanta Braves
"""
docs.append(document)

document = """
literal:
 |
 Boston Red Sox
 Detroit Tigers
 New York Yankees
folded:
 >
 New York Mets
 Chicago Cubs
 Atlanta Braves
"""
docs.append(document)

for doc in docs:

    yaml_doc=yaml.load(doc)

    print("start type : {0}".format(type(yaml_doc)))

    print("#yaml style")
    print(yaml.dump(yaml_doc, default_flow_style=False))

    print("#yaml flow_style")  # JSONはYAMLのフロースタイルの一種?
    print(yaml.dump(yaml_doc))

    print("#json style")
    print(json.dumps(yaml_doc, sort_keys=True, indent=2))

    print("")
$ python3.6 test00.py
start type : <class 'list'>
#yaml style
- Ginyu
- Recumu
- chize

#yaml flow_style
[Ginyu, Recumu, chize]

#json style
[
  "Ginyu",
  "Recumu",
  "chize"
]

start type : <class 'dict'>
#yaml style
a: 1
b:
  c: 3
  d: 4

#yaml flow_style
a: 1
b: {c: 3, d: 4}

#json style
{
  "a": 1,
  "b": {
    "c": 3,
    "d": 4
  }
}

start type : <class 'dict'>
#yaml style
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves

#yaml flow_style
american: [Boston Red Sox, Detroit Tigers, New York Yankees]
national: [New York Mets, Chicago Cubs, Atlanta Braves]

#json style
{
  "american": [
    "Boston Red Sox",
    "Detroit Tigers",
    "New York Yankees"
  ],
  "national": [
    "New York Mets",
    "Chicago Cubs",
    "Atlanta Braves"
  ]
}

start type : <class 'dict'>
#yaml style
folded: 'New York Mets Chicago Cubs Atlanta Braves

  '
literal: 'Boston Red Sox

  Detroit Tigers

  New York Yankees

  '

#yaml flow_style
{folded: 'New York Mets Chicago Cubs Atlanta Braves

    ', literal: 'Boston Red Sox

    Detroit Tigers

    New York Yankees

    '}

#json style
{
  "folded": "New York Mets Chicago Cubs Atlanta Braves\n",
  "literal": "Boston Red Sox\nDetroit Tigers\nNew York Yankees\n"
}

$


(C)2004-2017 skunk nakamae (SKISS Document Tool Version 0.022)