hobbes
hobbes

Reputation: 467

How do I print specific values in a key:value list from a YAML file?

I've just switched over from bash scripting to python, an am trying to understand the language by writing some code; please excuse my python ignorance.

I'm trying to determine how to pull specific key, and values from a YAML file.

ex.

import yaml

stream = open('test.yaml', 'r')
data = yaml.load(stream)

abc = data['yaml_key']

gives me something like:

{'1': 'a', '2': 'b', '3': 'c'}

How do I print specific key:values? I was hoping that it would act like a tuple, and I could just do something like:

abc[0]

etc. But unfortunately, when I try to print out abc[#], it just prints out abc[#], but printing out abc, gives me the key:value list.

This is probably an easy one for anyone experienced in Python, but any input would be appreciated.

Upvotes: 1

Views: 6616

Answers (1)

hobbes
hobbes

Reputation: 467

Contribution belongs to Padraic for his comment, but for future reference, the correct method to loading key:values is:

abc["key_value"]

Also worth noting in his response, the following:

abc.keys() // list keys
abc.get("key_value") // get specific key value

Upvotes: 1

Related Questions