apdb = {'AP Database': [{'AP Type': '110H',
'Name': 'varagu',
'Public IP': '100.20.300.400',
'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
{'AP Type': '110H',
'Name': 'thinai',
'Public IP': '100.20.300.500',
'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
{'AP Type': '110H',
'Name': 'Ragi',
'Public IP': '100.20.300.600',
'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}
ap_database = apdb.get('AP Database')
apall = ap_database[0], ap_database[1], ap_database[2]
for ap in apall:
public = ap.__getitem__('Public IP')
name = ap.__getitem__('Name')
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
for ip in ip_list:
if ip == public:
print public + ' ' + name + ' ' + "Success"
我被困在这里了。我需要用‘公共’映射'ip‘。
预期结果:
结果1:(这需要存储在一个变量中,因为我需要将它发送到邮件正文中)
100.20.300.400瓦拉古成功
100.20.300.500成功
结果2:(这需要存储在变量中,也需要以单独的邮件发送)
100.20.300.600拉吉失败
发布于 2018-01-07 03:01:02
请参阅下面的一些内联注释
apdb = {'AP Database': [{'AP Type': '110H',
'Name': 'varagu',
'Public IP': '100.20.300.400',
'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
{'AP Type': '110H',
'Name': 'thinai',
'Public IP': '100.20.300.500',
'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
{'AP Type': '110H',
'Name': 'Ragi',
'Public IP': '100.20.300.600',
'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}
ap_database = apdb.get('AP Database')
# no need to do this since ap_database is already a list, so we can iterate over it.
# apall = ap_database[0], ap_database[1], ap_database[2]
# ip_list does not change, so we can define it once outside the loop
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
# let us add two more variables to hold the fail and success list
success_list = []
fail_list = []
for ap in ap_database:
# ap is a dict, so use ap.get('key') or ap['key'] instead of ap.__getitem__('key')
# note that ap.get('key') returns None if key does not exist, while ap['key'] throws an error.
public = ap.get('Public IP')
name = ap.get('Name')
# check whether public is in ip_list and print appropriate message
if public in ip_list:
print public, name, 'Success'
success_list.append(ap)
else:
print public, name, 'Fail'
fail_list.append(ap)
发布于 2018-01-07 02:55:56
有很多方法可以达到你想要做的事情。
在解析apdb时,您可以检查'Public‘是否在ip_list
中,然后追加failed_ip
或succeed_ip
列表。
然后,您只需处理这两个列表:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
apdb = {'AP Database': [{'AP Type': '110H',
'Name': 'varagu',
'Public IP': '100.20.300.400',
'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
{'AP Type': '110H',
'Name': 'thinai',
'Public IP': '100.20.300.500',
'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
{'AP Type': '110H',
'Name': 'Ragi',
'Public IP': '100.20.300.600',
'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}
ap_database = apdb.get('AP Database')
apall = ap_database[0], ap_database[1], ap_database[2]
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
succeed_ip = []
failed_ip = []
for ap in apall:
success = None
failed = None
public = ap.__getitem__('Public IP')
name = ap.__getitem__('Name')
if public in ip_list:
succeed_ip.append((public, name))
continue
failed_ip.append((public, name))
print succeed_ip
print failed_ip
table = PrettyTable(['IP', 'Name', 'Result'])
table.add_row(succeed_ip[0])
table.add_row(succeed_ip[1])
print(table)
ftable = PrettyTable(['IP', 'Name', 'Result'])
ftable.add_row(failed_ip[0])
print(ftable)
结果:
[('100.20.300.400', 'varagu', 'success'), ('100.20.300.500', 'thinai', 'success')]
[('100.20.300.600', 'Ragi', 'Failed')]
+----------------+--------+---------+
| IP | Name | Result |
+----------------+--------+---------+
| 100.20.300.400 | varagu | success |
| 100.20.300.500 | thinai | success |
+----------------+--------+---------+
+----------------+------+--------+
| IP | Name | Result |
+----------------+------+--------+
| 100.20.300.600 | Ragi | Failed |
+----------------+------+--------+
发布于 2018-01-07 02:58:34
我已经稍微修改了您的代码: 1) apall = ap_database[0], ap_database[1], ap_database[2]
这个分配是不必要的,因为ap_database = apdb.get('AP Database')
这将使ap_database已经成为一个列表
2) ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
--这应该在循环之外,不需要为每个循环周期重新分配
3)您可以直接在ip_list:if public in ip_list:
中搜索公共ip,而不是下面的2行
for ip in ip_list:
if ip == public:
4)与使用getitem不同,只使用get方法对我来说更易读(如果您愿意的话,可以使用getitem)
apdb = {'AP Database': [{'AP Type': '110H',
'Name': 'varagu',
'Public IP': '100.20.300.400',
'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
{'AP Type': '110H',
'Name': 'thinai',
'Public IP': '100.20.300.500',
'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
{'AP Type': '110H',
'Name': 'Ragi',
'Public IP': '100.20.300.600',
'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}
ap_database = apdb.get('AP Database')
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
results = {'success': [], 'fail': []}
for ap in ap_database:
public = ap.get('Public IP')
name = ap.get('Name')
if public in ip_list:
results['success'].append(public)
else:
results['fail'].append(public)
print(results['success'], reuslts['fail'])
https://stackoverflow.com/questions/48136280
复制