Jamal Khan Jamal Khan - 3 months ago 13
YAML Question

YAML parsing and Python?

What is the best way to parse a YAML file into a Python object?

For example, this YAML:

Person:
name: XYZ


To this Python class:

class Person(yaml.YAMLObject):
yaml_tag = 'Person'

def __init__(self, name):
self.name = name


I am using PyYAML by the way.

Answer

If your YAML file looks like this:

# tree format
treeroot:
    branch1:
        name: Node 1
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1

And you've installed PyYAML like this:

pip install PyYAML

And the Python code looks like this:

import yaml
f = open('tree.yaml')
# use safe_load instead load
dataMap = yaml.safe_load(f)
f.close()

The variable dataMap now contains a dictionary with the tree data. If you print dataMap using PrettyPrint, you will get something like:

{'treeroot': {'branch1': {'branch1-1': {'name': 'Node 1-1'},
    'name': 'Node 1'},
    'branch2': {'branch2-1': {'name': 'Node 2-1'},
    'name': 'Node 2'}}}

So, now we have seen how to get data into our Python program. Saving data is just as easy:

f = open('newtree.yaml', "w")
yaml.dump(dataMap, f)
f.close()

You have a dictionary, and now you have to convert it to a Python object:

class Struct:
    def __init__(self, **entries): 
        self.__dict__.update(entries)

Then you can use:

>>> args = your YAML dictionary
>>> s = Struct(**args)
>>> s
<__main__.Struct instance at 0x01D6A738>
>>> s...

and follow "Convert Python dict to object".

For more information you can look at pyyaml.org and this.