How to parse/read a YAML file into a Python object? [duplicate]

Posted on

Question :

How to parse/read a YAML file into a Python object? [duplicate]

How to parse/read 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): = name

I am using PyYAML by the way.

Answer #1:

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
with open('tree.yaml') as f:
    # 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:

with open('newtree.yaml', "w") as f:
    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 and this.

Answered By: user702846

Answer #2:


add_path_resolver(tag, path, kind) adds a path-based implicit tag resolver. A path is a list of keys that form a path to a node in the representation graph. Paths elements can be string values, integers, or None. The kind of a node can be str, list, dict, or None.

#!/usr/bin/env python
import yaml

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

  def __init__(self, name): = name

yaml.add_path_resolver('!person', ['Person'], dict)

data = yaml.load("""
  name: XYZ

print data
# {'Person': <__main__.Person object at 0x7f2b251ceb10>}

print data['Person'].name
Answered By: user2393229

Answer #3:

Here is one way to test which YAML implementation the user has selected on the virtualenv (or the system) and then define load_yaml_file appropriately:

load_yaml_file = None

if not load_yaml_file:
        import yaml
        load_yaml_file = lambda fn: yaml.load(open(fn))

if not load_yaml_file:
    import commands, json
    if commands.getstatusoutput('ruby --version')[0] == 0:
        def load_yaml_file(fn):
            ruby = "puts YAML.load_file('%s').to_json" % fn
            j = commands.getstatusoutput('ruby -ryaml -rjson -e "%s"' % ruby)
            return json.loads(j[1])

if not load_yaml_file:
    import os, sys
    print """
ERROR: %s requires ruby or python-yaml  to be installed.

apt-get install ruby


apt-get install python-yaml


Demonstrate your mastery of Python by using pip.
Please research the latest pip-based install steps for python-yaml.
Usually something like this works:
   apt-get install epel-release
   apt-get install python-pip
   apt-get install libyaml-cpp-dev
   python2.7 /usr/bin/pip install pyyaml
Non-base library (yaml) should never be installed outside a virtualenv.
"pip install" is permanent:
Beware when using pip within an aptitude or RPM script.
  Pip might not play by all the rules.
  Your installation may be permanent.
Ruby is 7X faster at loading large YAML files.
pip could ruin your life.
Never use PyYaml in numerical applications.
If you are working for a Fortune 500 company, your choices are
1. Ask for either the "ruby" package or the "python-yaml"
package. Asking for Ruby is more likely to get a fast answer.
2. Work in a VM. I highly recommend Vagrant for setting it up.

""" % sys.argv[0]

# test
import sys
print load_yaml_file(sys.argv[1])
Answered By: personal_cloud

Leave a Reply

Your email address will not be published.