YAMLって何?

YAMLは、データ構造の書き方です。

  • 正式名称 : YAML Ain't a Markup Language(「Markup Language」と書いてあってもマークアップ言語ではありません)
  • 読み方 : ヤムル
  • 拡張子 : .yaml

JSONと同様にYAMLはキーと値を持つが、日付や時刻を始めとして、JSONよりも多くのデータ型を処理する。
https://www.oreilly.co.jp/books/images/picture978-4-87311-738-6.gif

e-words.jp

いろんな開発言語で使えます。

yaml.org

PythonにはPyYAMLというYAMLを扱うライブラリがあります。

PyYAML以外にもruamel.yamlやPySyck などがあります。

入門Python3のサンプルコードでYAMLに触れてみます。

https://www.oreilly.co.jp/books/images/picture978-4-87311-738-6.gif

これが、YAMLのファイルです。

YAMLには「ハッシュ」「配列」「スカラー」の3つの書き方を組み合わせてデータを表現します。

「キー: 値」形式で表されるのもがハッシュです。

「:」のあとには半角スペースを必ず入れます。

name:
  first: James
  last: McIntyre
「- 値とかキー」形式で配列になります。

「-」のあとには半角スペースを必ず入れます。

poems:
  - title: 'Motto'
    text: |
      Politeness, perseverance and pluck,
      To their possessor will bring good luck.
  - title: 'Canadian Charms'
    text: |
      Here industry is not in vain,
      For we have bounteous crops of grain,
      And you behold on every field
      Of grass and roots abundant yield,
      But after all the greatest charm
      Is the snug home upon the farm,
      And stone walls now keep cattle warm.
値がスカラーです。

true, false, yes, no, on, offは、Pythonで読み込むと真偽値となります。
整数と文字列は、Pythonで読み込むとPythonの整数と文字列になります。

dates:
  birth: 1828-05-25
  death: 1906-03-31
details:
  bearded: true
  themes: [cheese, Canada]

PyYMLをインストールします。

# 仮想環境をつくって、
$ python3 -m venv tryYaml
$ source tryYaml/bin/activate
(tryYaml) $ cd tryYaml/

# PyYAMLをインストールします。
$ pip3 install PyYAML
Collecting PyYAML
  Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)
    100% |████████████████████████████████| 276kB 1.1MB/s 
Installing collected packages: PyYAML
  Running setup.py install for PyYAML ... done
Successfully installed PyYAML-3.13

$ pip list
Package    Version
---------- -------
pip        19.0.1   
PyYAML     3.13   
setuptools 39.0.1 

# YAMLファイルをGitHubからダウンロードします。
(tryYaml) $ wget https://raw.githubusercontent.com/madscheme/introducing-python/master/storage/mcintyre.yaml
--2019-01-31 00:43:54--  https://raw.githubusercontent.com/madscheme/introducing-python/master/storage/mcintyre.yaml
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 151.101.108.133
raw.githubusercontent.com (raw.githubusercontent.com)|151.101.108.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 654 [text/plain]
`mcintyre.yaml' に保存中

mcintyre.yaml                                100%[===========================================================================================>]     654  --.-KB/s 時間 0s       

2019-01-31 00:43:54 (13.0 MB/s) - `mcintyre.yaml' へ保存完了 [654/654]

(tryYaml) $ ls -l
total 136
drwxr-xr-x@ 12 mana  staff  384  1 30 23:54 bin
drwxr-xr-x@  2 mana  staff   64  1 30 23:52 include
drwxr-xr-x@  3 mana  staff   96  1 30 23:46 lib
-rw-r--r--@  1 mana  staff  654  1 31 00:43 mcintyre.yaml
-rw-r--r--@  1 mana  staff   61  1 30 23:53 pip-selfcheck.json
-rw-r--r--@  1 mana  staff  114  1 30 23:52 pyvenv.cfg

コードを書きます。

# ソースファイルを作って
(tryYaml) $ echo -n > tryyml.py

# SublimeTextで開きます。
(tryYaml) $ subl tryyml.py 


実行するとYAMLファイルの内容を読み込めたことがわかります。

(tryYaml) $ python3 tryyml.py 
detailsの内容: {'bearded': True, 'themes': ['cheese', 'Canada']}
poemsの数: 2
poemsの1つ目のタイトル: Motto
datesの型: <class 'dict'>
datesのbirthの型: <class 'datetime.date'>
detailsのbeardedの型: <class 'bool'>
detailsのthemesの型: <class 'list'>
booksのurlの型: <class 'str'>
poemsのtextの型: <class 'str'>

(tryYaml) $ 

思い出の一枚

f:id:ponsuke_tarou:20190131201017j:plain
偕楽園