简体   繁体   English

如何从YAML文件中的key:value列表中打印特定值?

[英]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.

相关问题 如何在 Python 中为 YAML 文件的特定键添加值? - How can I add a value to a specific key to a YAML file in Python? 如何使用 Python 从 YAML 中的特定键中删除值? - How can I remove a value from a specific key in YAML with Python? 如何迭代 2 JSON 列表(混合字典和列表),匹配特定值并打印另一个值 - How do I iterate over 2 JSON lists (mix dict and list), match specific values and print another value 如何从每个键有多个值的字典中的键中获取特定值? - How do I get a specific value from a key in a dictionary with multiple values per key? 如何从列表中查找并打印出特定值(至少我认为那是我想做的事?) - How to find and print out of a specific value from a list (at least i think thats what i want to do?) 如何打印 YAML 字符串的特定部分 - How do I print a specific part of a YAML string 如何在字典列表中找到特定键的唯一值? - How do I find unique values for a specific key in a list of dictionaries? 如何创建字典,其中每个键都是列表中的一个值,而每个值都是列表中除键之外的所有值 - How do I create a dictionary where every key is a value from a list, and every value is all the values from the list except the key 如何从 json 请求打印特定值? - How do I print specific values from a json request? 如何从字典列表中提取特定键的所有值? - How do I extract all the values of a specific key from a list of dictionaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM