Jamal Khan Jamal Khan - 1 year ago 142
YAML Question

YAML parsing and Python?

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

For example, this YAML:

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 Source

If your YAML file looks like this:

# tree format
        name: Node 1
            name: Node 1-1
        name: Node 2
            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)

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)

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

class Struct:
    def __init__(self, **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.