[英]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; 我刚刚从bash脚本切换到python,我正试图通过编写一些代码来理解语言。 please excuse my python ignorance. 请原谅我的python无知。
I'm trying to determine how to pull specific key, and values from a YAML file. 我正在尝试确定如何从YAML文件中提取特定的键和值。
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? 如何打印特定的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. 等等,但是不幸的是,当我尝试打印abc [#]时,它只是打印abc [#],但是打印abc却给了我key:value列表。
This is probably an easy one for anyone experienced in Python, but any input would be appreciated. 对于任何有Python经验的人来说,这可能都是一个简单的方法,但是任何输入都会受到赞赏。
Contribution belongs to Padraic for his comment, but for future reference, the correct method to loading key:values is: 贡献属于Padraic的评论,但为将来参考,加载key:values的正确方法是:
abc["key_value"]
Also worth noting in his response, the following: 同样值得一提的是,他的回答如下:
abc.keys() // list keys
abc.get("key_value") // get specific key value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.