Import values from a Dictionary to my Python variables

0
Inventory_data = {"Inventario_datos": {"Alti MW $ 2 |": {"Floor_price": '2.5', "Li_platform": 1,
                                                    "Excel_name": "Alti MW $ 2.xlsx", "Li_Publisher": 2,
                                                    "Tag_Rate": "2", "TagName": "Alti MW $ 2 |"}},
                                      "Alti MW $ 3 |": {"Floor_price": '2.75', "Li_platform": 1,
                                                    "Excel_name": "Alti MW $ 2.25.xlsx", "Li_Publisher": 2,
                                                    "Tag_Rate": "2.25", "TagName": "Alti MW $ 2.25 |"}}

With that dictionary I am wanting to work. I seek to copy these values within my variables:

Escriba el campo = "Alti MW $ 2 |"

#Packs para completar
TagName = "{}"
Floor_price = '{}'
Li_platform = '// * [@ id = "platform_listbox"] / li [{}]'
Excel_name = '{}'
Li_Publisher = '// * [@ id = "publisher_listbox"] / li [{}]'
Tag_Rate = '{}'

In write field the idea is that one places in this case "Alti MW $ 2 |" or "Alti MW $ 3 |" and that in my variables, within each {} the dictionary data is copied.

To be clear, I need a function where you specify "Alti MW $ 3 |" and I returned all the values in the variables below. It would have to look something like this:

Escriba el campo = "Alti MW $ 2 |"
#Packs para completar
TagName = "Alti MW $ 2 |"
Floor_price = '2.8'
Li_platform = '// * [@ id = "platform_listbox"] / li [1]'
Excel_name = 'Alti MW $ 2.25.xlsx'
Li_Publisher = '// * [@ id = "publisher_listbox"] / li [2]'
    
asked by Martin Bouhier 06.10.2017 в 18:40
source

1 answer

1

By starting your dictionary seems badly written or defined, the key "Alti MW $ 3 |" is at the same level as "Inventario_datos" I understand that it should be at the same level as "Alti MW $ 2 |" , check this I say, because the solution is conditioned to that the dictionary is actually like this:

Inventory_data = {"Inventario_datos": {"Alti MW $ 2 |": {"Floor_price": '2.5', "Li_platform": 1,
                                                    "Excel_name": "Alti MW $ 2.xlsx", "Li_Publisher": 2,
                                                    "Tag_Rate": "2", "TagName": "Alti MW $ 2 |"},
                                      "Alti MW $ 3 |": {"Floor_price": '2.75', "Li_platform": 1,
                                                    "Excel_name": "Alti MW $ 2.25.xlsx", "Li_Publisher": 2,
                                                    "Tag_Rate": "2.25", "TagName": "Alti MW $ 2.25 |"}
                                      }
}

Well, what you can do is create a routine that reads the dictionary and processes it by returning every value required in your example. Something like this:

def load_vars(key):
  d = (Inventory_data.get("Inventario_datos")).get(key)
  if d:
    return d.get("Floor_price", None), d.get("Li_platform", None), d.get("Excel_name", None), d.get("Li_Publisher", None) , d.get("Tag_Rate", None) , d.get("TagName", None)
  else:
    return None, None, None, None, None, None

This routine also controls that the dictionary referenced by the parameter key exists, otherwise it will return all values as None , or you could also throw an exception in that case and control the routine with a block try . Also, if there is not any of the expected values in the dictionary, we could return a "default" value since just get() allows us to do this.

    
answered by 06.10.2017 / 19:42
source