对从 xlrd 中提取的列表中的 IP 地址进行配对
Pair IP addresses in a list pulled from xlrd
我已经使用 xlrd 从列(下面的数据)中提取数据。我需要将 ip 地址组合在一起。因此,输出中下一个一起出现的 ip 地址属于同一个池,而单个地址则在它们自己的池中。例如 (10.100.33.183,10.100.33.184) 属于 (pool1)。 (Pool6 =10.100.33.89)
我该如何实现这个所有帮助欢迎。
['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
ip_data = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
ip_pools = [[]] # it starts as a list with an empty list at its last (and only) index
for ip_address in ip_data[ip_data.index('Pool Member IP')+1:]:
if not ip_address: # ip_address is ''
if ip_pools[-1]: # the last element of ip_pools is NOT an empty list: []
ip_pools.append([]) # for the next ip pool
else: # ip_address is not empty
# ip_pools[-1].append(ip_address) # if you need the whole text
ip_pools[-1].append(ip_address.partition(' ')[0]) # if you just want the number
if [] in ip_pools:
ip_pools.remove([]) # to remove last empty list (if exists)
编辑:更正了 for 句子
@franciscosollima 的解决方案很好。这是使用正则表达式的另一种方式。
iplist = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
import re
p = re.compile('[\d]+(?:\.[\d]+){3}')
pools = [[]]
for ip in iplist:
m = p.match(ip)
if m:
pools[-1].append(m.group(0))
elif not pools[-1]:
continue
else:
pools.append([])
if [] in pools:
pools.remove([])
for i, p in enumerate(pools, 1):
print("Group " + str(i) +": " + str(p))
就像将连续的匹配项添加到同一池中一样简单。否则,初始化一个新的。正则表达式模式将从一开始就匹配,您也可以配置它检测 IPv6 地址。
打印出来:
Group 1: ['10.100.33.184', '10.100.33.183']
Group 2: ['10.101.33.182', '10.100.33.181']
Group 3: ['10.100.33.180', '10.100.33.179']
Group 4: ['10.100.33.178', '10.100.33.177']
Group 5: ['10.100.33.90']
Group 6: ['10.100.33.89']
Group 7: ['10.100.33.91']
ips = [ip.split()[0] for ip in data if ip[0].isdigit()]
sort = sorted(ips, key= lambda ip: int(ip.split('.')[-1]))
i, l, c = 0, len(sort), 1
pools = {}
while i < l:
if int(sort[i].split('.')[-1]) == int(sort[i+1]).split('.')[-1])-1:
pools[c] = (sort[i], sort[i+1])
i += 2
else:
pools[c] = (sort[i],)
i += 1
c += 1
我可以玩一下 itertools 以获得答案吗?
test = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
import itertools
def isplit(iterable,splitters):
return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]
test.remove('Pool Member IP')
pool = 0
for list in isplit(test,''):
if len(list):
pool+=1
print(pool, list)
打印出来:
1 ['10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)']
2 ['10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)']
3 ['10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)']
4 ['10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)']
5 ['10.100.33.90 (S56723FR6VL09)']
6 ['10.100.33.89 (S56723FR6VL0A)']
7 ['10.100.33.91 (S56723FR6VW01)']
感谢Split a list into nested lists on a value和google-fu
我已经使用 xlrd 从列(下面的数据)中提取数据。我需要将 ip 地址组合在一起。因此,输出中下一个一起出现的 ip 地址属于同一个池,而单个地址则在它们自己的池中。例如 (10.100.33.183,10.100.33.184) 属于 (pool1)。 (Pool6 =10.100.33.89)
我该如何实现这个所有帮助欢迎。
['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
ip_data = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
ip_pools = [[]] # it starts as a list with an empty list at its last (and only) index
for ip_address in ip_data[ip_data.index('Pool Member IP')+1:]:
if not ip_address: # ip_address is ''
if ip_pools[-1]: # the last element of ip_pools is NOT an empty list: []
ip_pools.append([]) # for the next ip pool
else: # ip_address is not empty
# ip_pools[-1].append(ip_address) # if you need the whole text
ip_pools[-1].append(ip_address.partition(' ')[0]) # if you just want the number
if [] in ip_pools:
ip_pools.remove([]) # to remove last empty list (if exists)
编辑:更正了 for 句子
@franciscosollima 的解决方案很好。这是使用正则表达式的另一种方式。
iplist = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
import re
p = re.compile('[\d]+(?:\.[\d]+){3}')
pools = [[]]
for ip in iplist:
m = p.match(ip)
if m:
pools[-1].append(m.group(0))
elif not pools[-1]:
continue
else:
pools.append([])
if [] in pools:
pools.remove([])
for i, p in enumerate(pools, 1):
print("Group " + str(i) +": " + str(p))
就像将连续的匹配项添加到同一池中一样简单。否则,初始化一个新的。正则表达式模式将从一开始就匹配,您也可以配置它检测 IPv6 地址。
打印出来:
Group 1: ['10.100.33.184', '10.100.33.183']
Group 2: ['10.101.33.182', '10.100.33.181']
Group 3: ['10.100.33.180', '10.100.33.179']
Group 4: ['10.100.33.178', '10.100.33.177']
Group 5: ['10.100.33.90']
Group 6: ['10.100.33.89']
Group 7: ['10.100.33.91']
ips = [ip.split()[0] for ip in data if ip[0].isdigit()]
sort = sorted(ips, key= lambda ip: int(ip.split('.')[-1]))
i, l, c = 0, len(sort), 1
pools = {}
while i < l:
if int(sort[i].split('.')[-1]) == int(sort[i+1]).split('.')[-1])-1:
pools[c] = (sort[i], sort[i+1])
i += 2
else:
pools[c] = (sort[i],)
i += 1
c += 1
我可以玩一下 itertools 以获得答案吗?
test = ['', '', '', '', '', '', '', 'Pool Member IP', '', '10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)', '', '', '', '', '', '', '', '10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)', '', '', '', '', '', '', '', '10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)', '', '', '', '', '', '', '', '10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)', '', '', '', '', '', '', '', '10.100.33.90 (S56723FR6VL09)', '', '', '', '', '', '', '', '', '10.100.33.89 (S56723FR6VL0A)', '', '', '', '', '', '', '', '', '10.100.33.91 (S56723FR6VW01)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
import itertools
def isplit(iterable,splitters):
return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]
test.remove('Pool Member IP')
pool = 0
for list in isplit(test,''):
if len(list):
pool+=1
print(pool, list)
打印出来:
1 ['10.100.33.184 (S56723FR6VL01)', '10.100.33.183 (S56723FR6VL02)']
2 ['10.101.33.182 (S56723FR6VL03)', '10.100.33.181 (S56723FR6VL04)']
3 ['10.100.33.180 (S56723FR6VL05)', '10.100.33.179 (S56723FR6VL06)']
4 ['10.100.33.178 (S56723FR6VL07)', '10.100.33.177 (S56723FR6VL08)']
5 ['10.100.33.90 (S56723FR6VL09)']
6 ['10.100.33.89 (S56723FR6VL0A)']
7 ['10.100.33.91 (S56723FR6VW01)']
感谢Split a list into nested lists on a value和google-fu