YAML is an alternative to XML that has a cleaner, more Pythonic way of representing structured data. Instead of ugly delimiters, it uses indentation to represent nesting of data structures.
Like Python, which has a few basic types (see BasicPythonTypes), YAML has only three basic types: maps, sequences, and scalars.
SteveHowell posted a simple PythonYamlEmitter here a while back.
He later built a more complete YAML implementation, although he never made it fully spec-compliant. His code can be found here:
Here is what YAML looks like:
---
# A1
yaml: |
- Mark McGwire
- Sammy Sosa
- Ken Griffey
python: |
[ ['Mark McGwire', 'Sammy Sosa', 'Ken Griffey'] ]
---
# A2
yaml: |
hr: 65
avg: 0.278
rbi: 147
python: |
[ {'hr': 65, 'avg': .278, 'rbi': 147} ]
---
# A3
yaml: |
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
- Texas Rangers
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
- Montreal Expos
python: |
[
{
'american':
['Boston Red Sox', 'Detroit Tigers',
'New York Yankees', 'Texas Rangers'],
'national':
['New York Mets', 'Chicago Cubs',
'Atlanta Braves', 'Montreal Expos']
}
]
---
# A4
yaml: |
-
name: Mark McGwire
hr: 65
avg: 0.278
rbi: 147
-
name: Sammy Sosa
hr: 63
avg: 0.288
rbi: 141
python: |
[[
{
'name': 'Mark McGwire',
'hr': 65,
'avg': 0.278,
'rbi': 147
},
{
'name': 'Sammy Sosa',
'hr': 63,
'avg': 0.288,
'rbi': 141
}
]]
---
# A5
yaml: |
?
- New York Yankees
- Atlanta Braves
:
- 2001-07-02
- 2001-08-12
- 2001-08-14
?
- Detroit Tigers
- Chicago Cubs
:
- 2001-07-23
python: |
[
{
('New York Yankees', 'Atlanta Braves'):
['2001-07-02', '2001-08-12', '2001-08-14'],
('Detroit Tigers', 'Chicago Cubs'):
['2001-07-23']
}
]
---
# A6
yaml: |
invoice: 34843
date : 2001-01-23
bill-to:
given : Chris
family : Dumars
product:
- quantity: 4
desc : Basketball
- quantity: 1
desc : Super Hoop
python: |
[{
'invoice': 34843,
'date': '2001-01-23',
'bill-to': {
'given': 'Chris',
'family': 'Dumars',
},
'product': [
{'quantity': 4, 'desc': 'Basketball'},
{'quantity': 1, 'desc': 'Super Hoop'}
]
}]
---
# B1
yaml: |
---
name: Mark McGwire
hr: 65
avg: 0.278
rbi: 147
---
name: Sammy Sosa
hr: 63
avg: 0.288
rbi: 141
python: |
[
{
'name': 'Mark McGwire',
'hr': 65,
'avg': 0.278,
'rbi': 147
},
{
'name': 'Sammy Sosa',
'hr': 63,
'avg': 0.288,
'rbi': 141
}
]
---
# B2
yaml: |
# Ranking of players by
# season home runs.
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey
python: |
[
['Mark McGwire', 'Sammy Sosa', 'Ken Griffey']
]
---
# B3
yaml: |
hr: # Home runs
# 1998 record
- Mark McGwire
- Sammy Sosa
rbi: # Runs batted in
- Sammy Sosa
- Ken Griffey
python: |
[
{
'hr' : ['Mark McGwire', 'Sammy Sosa'],
'rbi' : ['Sammy Sosa', 'Ken Griffey']
}
]
---
# B4
yaml: |
hr:
- Mark McGwire
# Name "Sammy Sosa" scalar SS
- &SS Sammy Sosa
rbi:
# So it can be referenced later.
- *SS
- Ken Griffey
python: |
[
{
'hr' : ['Mark McGwire', 'Sammy Sosa'],
'rbi' : ['Sammy Sosa', 'Ken Griffey']
}
]
---
# C1
yaml: |
--- >
Mark McGwire's
year was crippled
by a knee injury.
python: |
[ "Mark McGwire's year was crippled by a knee injury.\n" ]
---
# C2
yaml: |
--- |
\/|\/|
/ | |_
python: |
[
here(
"""
\/|\/|
/ | |_
"""
)
]
---
# C3
yaml: |
--- >
Sammy Sosa completed another
fine season with great stats.
63 Home Runs
0.288 Batting Average
What a year!
python: |
[
here(
"""
Sammy Sosa completed another fine season with great stats.
63 Home Runs
0.288 Batting Average
What a year!
"""
)
]
---
# C4
yaml: |
name: Mark McGwire
accomplishment: >
Mark set a major league
home run record in 1998.
stats: |
65 Home Runs
0.278 Batting Average
python: |
[
{
'name': 'Mark McGwire',
'accomplishment':
'Mark set a major league home run record in 1998.\n',
'stats': "65 Home Runs\n0.278 Batting Average\n"
}
]
Rumor has it that BrianIngerson has started a new Python port of YAML in February 2004.