因為其語言特性,可以讓人眼很容易且清楚的識別其資訊。
特色
它使用空白符號縮排和大量依賴外觀的特色,特別適合用來表達或編輯
- 資料結構
- 各種設定檔
- 傾印除錯內容
- 文件大綱(例如:許多電子郵件標題格式和YAML非常接近)。
- 儘管它比較適合用來表達階層式(hierarchical model)的資料結構,不過也有精緻的語法可以表示關聯性(relational model)的資料。
- 由於YAML使用空白字元和分行來分隔資料,所以特別適用於script language (Ex: grep/Python/Perl/Ruby)。
- 其讓人最容易上手的特色是巧妙避開各種封閉符號 (EX: 引號、各種括號等,這些符號在巢狀結構時會變得複雜而難以辨認。
安裝
2. 解壓至PyYMAL-3.09
3. cd PyYMAL-3.09
4. 執行下列指令
python setup.py install
範例
# 筆者特別愛用 Python 來使用YAML. 因為資料結構可直接套用。
# yaml_test1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yaml | |
document = """ | |
a: 1 | |
b: | |
c: 3 | |
d: 4 | |
""" | |
r_load=yaml.dump(yaml.load(document)) | |
print(r_load) | |
r_dump=yaml.dump({'name': "The Cloak 'Colluin'", | |
'depth': 5, 'rarity': 45, | |
'weight': 10, | |
'cost': 50000, | |
'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}) | |
print(r_dump) |
執行結果
$ python py_yaml_test.py
a: 1
b: {c: 3, d: 4}
cost: 50000
depth: 5
flags: [INT, WIS, SPEED, STEALTH]
name: The Cloak 'Colluin'
rarity: 45
weight: 10
|
Reference for PyYAML
更新
- update syntax 已測試 Python 2.7.12, Python 3.5.2