【问题标题】:Obtaining dictionary names in a yaml file获取 yaml 文件中的字典名称
【发布时间】:2021-08-02 09:44:51
【问题描述】:

我有一个如下所示的 yaml 文件:

Histograms:
  zee: 
    bins: {"fix": [55, 0.1 ,35e3]}  
    Title: "é isso ai"
    Xlabel: "como assim" 
    Ylabel: "Z #rightarrow e^{+}e^{-}"
    ymax: 35e3
    ymim: 0.1  
    color: 4 #root.kAzure+3 
    scale: 1.0

  ztautau: 
    ymax: 35e3
    ymim: 0.1    
    ymaxp: 1.19
    ymimp: 0.81
    label: "Z #rightarrow e^{+}e^{-}"     
    color: ""

  ttbar: 
    ymax: 35e3
    ymim: 0.1    
    ymaxp: 1.19
    ymimp: 0.81
    label: "Z #rightarrow e^{+}e^{-}"    
    
  wplustaunu: 
     bins: {"fix": [55, 0.1 ,35e3]}  
     Title: "é isso ai"
     Xlabel: "como assim" 
     Ylabel: "Z #rightarrow e^{+}e^{-}"
     ymax: 35e3
     ymim: 0.1  
     color: "root.kAzure+3" 
     scale: 1.0   

我需要通过循环获取以下字符串:zee,ztautau,ttbar,wplustaunu(我猜它们是字典的标题?)。我的意思是我需要通过一个循环(在 python 中)来处理它们,因为我需要处理有很多这样的字典的文件,所以我自己知道它们不是一个好的选择。但是我一直在研究这个问题并且无法找到解决方案,所以如果有人可以帮助我或给我一些参考,我将非常感激。

我想要的输出是:


兹陶陶
ttbar
wplusenu

我不想要更多,只是这些数据。有可能吗?

感谢关注!

【问题讨论】:

  • 向我们展示您期望的输出。获取字符串很容易,但我想还有更多。是否要将子条目作为嵌套字典?
  • 而且,顺便说一下,还有一个用于 Python 的 YAML 解析器:pyyaml.org/wiki/PyYAMLDocumentation

标签: python list dictionary yaml


【解决方案1】:

您只需要在直方图下过滤掉什么是字典。您可以使用dictionary comprehension

from yaml import safe_load as yload

di = yload("""    Histograms:
      zee: 
        bins: {"fix": [55, 0.1 ,35e3]}  
        Title: "é isso ai"
        Xlabel: "como assim" 
        Ylabel: "Z #rightarrow e^{+}e^{-}"
        ymax: 35e3
        ymim: 0.1  
        color: 4 #root.kAzure+3 
        scale: 1.0
    
      ztautau: 
        ymax: 35e3
        ymim: 0.1    
        ymaxp: 1.19
        ymimp: 0.81
        label: "Z #rightarrow e^{+}e^{-}"     
        color: ""
    
      ttbar: 
        ymax: 35e3
        ymim: 0.1    
        ymaxp: 1.19
        ymimp: 0.81
        label: "Z #rightarrow e^{+}e^{-}"    
        
      wplustaunu: 
         bins: {"fix": [55, 0.1 ,35e3]}  
         Title: "é isso ai"
         Xlabel: "como assim" 
         Ylabel: "Z #rightarrow e^{+}e^{-}"
         ymax: 35e3
         ymim: 0.1  
         color: "root.kAzure+3" 
         scale: 1.0   
""")

dict_of_dict = {k:v for k,v in di.get("Histograms",{}).items() if isinstance(v, dict)}

for k,v in dict_of_dict.items():  
    print(f"\n{k}:{v}")

输出:

zee:{'bins': {'fix': [55, 0.1, '35e3']}, 'Title': 'é isso ai', 'Xlabel': 'como assim', 'Ylabel': 'Z #rightarrow e^{+}e^{-}', 'ymax': '35e3', 'ymim': 0.1, 'color': 4, 'scale': 1.0}

ztautau:{'ymax': '35e3', 'ymim': 0.1, 'ymaxp': 1.19, 'ymimp': 0.81, 'label': 'Z #rightarrow e^{+}e^{-}', 'color': ''}

ttbar:{'ymax': '35e3', 'ymim': 0.1, 'ymaxp': 1.19, 'ymimp': 0.81, 'label': 'Z #rightarrow e^{+}e^{-}'}

wplustaunu:{'bins': {'fix': [55, 0.1, '35e3']}, 'Title': 'é isso ai', 'Xlabel': 'como assim', 'Ylabel': 'Z #rightarrow e^{+}e^{-}', 'ymax': '35e3', 'ymim': 0.1, 'color': 'root.kAzure+3', 'scale': 1.0}

只有钥匙吗,先生?

[k for k,v in di.get("Histograms",{}).items() if isinstance(v, dict)]

【讨论】:

  • 感谢您的帮助,但我想要的唯一输出是:zee、ztautau、ttbar 和 wplusenu。我不想要它们里面的信息。
  • @Daumann 对答案的必要修改很少。
  • 是的,现在我明白了。非常感谢@JL Peyret 和 timgeb。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 2014-04-25
相关资源
最近更新 更多