Pacific-Design.com

    
Home Index

1. Python

2. YAML

Python / YAML /

Read YAML in Python

# Python YAML Library Installation
$ wget http://pyyaml.org/download/pyyaml/PyYAML-3.09.tar.gz
$ python setup.py install

#!/usr/local/bin/python
import pprint
import yaml

'''--- Open YAML File ---'''
f = open('tree.yaml')
tree = yaml.load(f)
f.close()

'''--- Print using pretty printer ---'''
print '----------------------------------------------------------------------------'
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(tree)

'''--- Print individual tree node ---'''
print '----------------------------------------------------------------------------'
print "tree['treeroot']['branch1']['name'] = " + tree['treeroot']['branch1']['name']
print '----------------------------------------------------------------------------'
# tree format
treeroot:
    branch1:
        name: Node 1
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1
References: