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", "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:
branch1: branch1 text
branch2: branch2 text
yaml.load() function maps 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
branch 1 key is under the