Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6186253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:51:14+00:00 2026-05-24T01:51:14+00:00

How to parse/read a YAML file into a Python object? For example, this YAML:

  • 0

How to parse/read 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:51:15+00:00Added an answer on May 24, 2026 at 1:51 am

    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
    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): 
            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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I parse and read an outlook pst file in Python?
Is it possible to emit and read(parse) binary data(image, file etc)? Like this is
Which is the best way to read and parse an Fixed Width Text File
I'm looking for a library to read a SWF file and let me parse
I am using HtmlAgilityPack to read a parse a html file and extract some
I've to read and parse HTML file and populate a data structure (in C++).
Does anyone know why this yaml is parsed and read correctly by my app
Is there a standard way to load, read and parse an XML file from
I'm building a script that utilizes a config file (YAML) to read in all
I’m trying to read and parse a UTF-8 Excel file using the PHPExcelReader but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.