Matthew D.
Matthew D.

Reputation: 363

Obtaining dictionary names in a yaml file

i have a yaml file like the below:

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   

And i need to obtain the folowing strings through a loop: zee,ztautau,ttbar,wplustaunu (they are the title of the dictionaries, i guess?). I mean i need they through a loop (in python) because i need to work with files that have lots of dictionaries like that, so witting them down myself is not a good choice. But i am been working on this for some time and was unable to figure out a solution, so if anyone can help me or point me some reference, i would be extremely grateful.

The output that i want is:

zee
ztautau
ttbar
wplusenu

I dont want anything more, just these data. Is it possible?.

Thanks for the attention!

Upvotes: 0

Views: 382

Answers (1)

JL Peyret
JL Peyret

Reputation: 12204

You just need to filter out what is a dictionary under Histograms. You can do with a 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}")

output:

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}

Just the keys, Sir?

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

Upvotes: 2

Related Questions