95

I am new to YAML and have been searching for ways to parse a YAML file and use/access the data from the parsed YAML.

I have come across explanations on how to parse the YAML file, for example, the PyYAML tutorial, "How can I parse a YAML file in Python", "Convert Python dict to object?", but what I haven't found is a simple example on how to access the data from the parsed YAML file.

Assume I have a YAML file such as:

 treeroot:
     branch1: branch1 text
     branch2: branch2 text

How do I access the text "branch1 text"?

"YAML parsing and Python?" provides a solution, but I had problems accessing the data from a more complex YAML file. And, I'm wondering if there is some standard way of accessing the data from a parsed YAML file, possibly something similar to "tree iteration" or "elementpath" notation or something which would be used when parsing an XML file?

Community
  • 1
  • 1
9monkeys
  • 1,754
  • 1
  • 14
  • 13
  • 1
    Possible duplicate of [How can I parse a YAML file in Python](https://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file-in-python) – fmsf Dec 06 '17 at 16:04

2 Answers2

164

Since PyYAML's yaml.load() function parses YAML documents to native Python data structures, you can just access items by key or index. Using the example from the question you linked:

import yaml
with open('tree.yaml', 'r') as f:
    doc = yaml.load(f)

To access branch1 text you would use:

txt = doc["treeroot"]["branch1"]
print txt
"branch1 text"

because, in your YAML document, the value of the branch1 key is under the treeroot key.

Aphex
  • 7,390
  • 5
  • 33
  • 54
  • 3
    I get "TypeError: string indices must be integers, not str". It seems I cannot use string for index. – sattu Dec 05 '16 at 17:24
  • You're likely trying to access (index into) a string. You might be going too deep; are you already at the element you're trying to access? – Aphex Dec 05 '16 at 21:31
  • @sattu I had the same error because I did not have any space between the key and the value, ie "labelsize: 20" instead of "labelsize:20" – mateuszb Jan 30 '18 at 16:43
12

Just an FYI on @Aphex's solution -

In case you run into -

"YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated"

you may want to use the Loader=yaml.FullLoader or Loader=yaml.SafeLoader option.

import yaml 

with open('cc_config.yml', 'r') as f:
    doc = yaml.load(f, Loader=yaml.FullLoader) # also, yaml.SafeLoader

txt = doc["treeroot"]["branch1"]
print (txt)
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Raja Sarkar
  • 131
  • 2
  • 3