Home > OS >  python how to convert <type 'str'> to <type 'dict'>
python how to convert <type 'str'> to <type 'dict'>

Time:05-18

I have a below dictionary defined with IP address of the application for the respective region. Region is user input variable, based on the input i need to procees the IP in rest of my script.

app_list=["puppet","dns","ntp"]

dns={'apac':["172.118.162.93","172.118.144.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]}
ntp={'asia':["172.118.162.93","172.118.148.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]}
puppet={'asia':["172.118.162.2251","1932.1625.254.2493"],'euro':["172.118.76.21","1932.1625.254.2493"],'cana':["172.118.76.21","193.1625.254.249"]}

Code Tried

   region=raw_input("entee the region:")
    for appl in app_list:
            for ip in appl[region]:
                   <<<rest of operations with IP in script>>

When I tried the above code got the error mentioned below. I tried to convert the str object to dict using json and ast module but still did not succeedd.

Error received

TypeError: string indices must be integers, not str

As new to python unsure how to get the ip list based on the region

CodePudding user response:

The problem is that in each loop for appl in app_list appl will be a string and you are trying to index appl[region], so you are trying to index the string with another string. But actually you just want to loop over the dictionaries dns, ntp and puppet, so you could do this instead:

dns={'apac':["172.118.162.93","172.118.144.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]}
ntp={'asia':["172.118.162.93","172.118.148.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]}
puppet={'asia':["172.118.162.2251","1932.1625.254.2493"],'euro':["172.118.76.21","1932.1625.254.2493"],'cana':["172.118.76.21","193.1625.254.249"]}

app_list = [puppet, dns, ntp]

region = input("entee the region:")

for appl in app_list:
  for ip in appl.get(region, []):
    print(ip)

Notice that 1) app_list is now a list of the predefined dictionaries and 2) that you should indx the region using the get() method for dictionaries, because the region might not be present in a given dictionary, so you can return an empty list as default. Otherwise you could run into a KeyError.

I hope that's what you were looking for.

CodePudding user response:

problem 1: your loop implies that app_list is a nested dict, which it isn't

problem 2: appl is a key to a dict, not the value of the dict.

this works:

app_list={
"puppet": {'asia':["172.118.162.2251","1932.1625.254.2493"],'euro':["172.118.76.21","1932.1625.254.2493"],'cana':["172.118.76.21","193.1625.254.249"]},
"dns": {'apac':["172.118.162.93","172.118.144.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]},
"ntp": {'asia':["172.118.162.93","172.118.148.93"],'euro':["172.118.76.93","172.118.204.93","172.118.236.93"],'cana':["172.118.48.93","172.118.172.93"]}
}

region=input("entee the region:")
print("")
for key in app_list:
  appl = app_list[key]
  if region in appl:
    for ip in appl[region]:
      print(ip)
  else:
    print("region not found in appl "   str(appl))

output

echo "asia" | python3 blubb.py
entee the region:
172.118.162.2251
1932.1625.254.2493
region not found in appl {'apac': ['172.118.162.93', '172.118.144.93'], 'euro': ['172.118.76.93', '172.118.204.93', '172.118.236.93'], 'cana': ['172.118.48.93', '172.118.172.93']}
172.118.162.93
172.118.148.93

  • Related