python 如何将 <type 'str'> 转换为 <type 'dict'>
python how to convert <type 'str'> to <type 'dict'>
我有一个下面的字典,其中定义了各个区域的应用程序的 IP 地址。区域是用户输入变量,基于我需要在脚本的其余部分处理 IP 的输入。
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"]}
已尝试代码
region=raw_input("entee the region:")
for appl in app_list:
for ip in appl[region]:
<<<rest of operations with IP in script>>
当我尝试上面的代码时,出现了下面提到的错误。我尝试使用 json 和 ast 模块将 str 对象转换为 dict 但仍然没有成功。
收到错误
TypeError: string indices must be integers, not str
新手python 不确定如何根据地区获取 ip 列表
问题 1:你的循环暗示 app_list 是一个嵌套的字典,而它不是
问题2:appl是字典的键,不是字典的值。
这个有效:
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))
输出
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
问题是在每个循环中 for appl in app_list
appl 将是一个字符串,而您正试图索引 appl[region]
,因此您正试图用另一个字符串索引该字符串。但实际上你只想遍历字典 dns
、ntp
和 puppet
,所以你可以这样做:
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)
请注意 1) app_list
现在是预定义词典的列表,并且 2) 您应该使用 get()
方法为词典索引该区域,因为该区域可能不存在于一个给定的字典,所以你可以 return 一个空列表作为默认值。否则你可以 运行 变成 KeyError
.
希望这就是您要找的。
我有一个下面的字典,其中定义了各个区域的应用程序的 IP 地址。区域是用户输入变量,基于我需要在脚本的其余部分处理 IP 的输入。
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"]}
已尝试代码
region=raw_input("entee the region:")
for appl in app_list:
for ip in appl[region]:
<<<rest of operations with IP in script>>
当我尝试上面的代码时,出现了下面提到的错误。我尝试使用 json 和 ast 模块将 str 对象转换为 dict 但仍然没有成功。
收到错误
TypeError: string indices must be integers, not str
新手python 不确定如何根据地区获取 ip 列表
问题 1:你的循环暗示 app_list 是一个嵌套的字典,而它不是
问题2:appl是字典的键,不是字典的值。
这个有效:
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))
输出
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
问题是在每个循环中 for appl in app_list
appl 将是一个字符串,而您正试图索引 appl[region]
,因此您正试图用另一个字符串索引该字符串。但实际上你只想遍历字典 dns
、ntp
和 puppet
,所以你可以这样做:
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)
请注意 1) app_list
现在是预定义词典的列表,并且 2) 您应该使用 get()
方法为词典索引该区域,因为该区域可能不存在于一个给定的字典,所以你可以 return 一个空列表作为默认值。否则你可以 运行 变成 KeyError
.
希望这就是您要找的。