2017-07-06 5 views
0

xlrdを使用してカラムからデータを取得しました(データは以下)。私は一緒にIPアドレスをグループ化する必要があります。そのため、出力内に次々に表示されるIPアドレスは同じプールに属し、単一のIPアドレスはそれぞれ独自のプールに属します。たとえば(10.100.33.183,10.100.33.184)は(pool1)に属しています。 (プール6 = 10.100.33.89)xlrdからプルされたリスト内のIPアドレスのペア

このすべてのヘルプを歓迎するにはどうすればよいですか。

[ ''、 ''、 ''、 ''、 ''、 ''、 ''、 'プールメンバーIP'、 ''、'10 .100.33.184(S56723FR6VL01)」、'10 .100.33.183 (S56723FR6VL03)」、「10.100.33.181(S56723FR6VL04)」、「 '」、「'」、「 '」、' ''、 ''、 ''、 '' (S56723FR6VL05)」、「10.100.33.179(S56723FR6VL06)」、「 ''、 ''、 ''、 ''、 ''、 ''、 ''、 ''、 ''、 '' 、 ''、 ''、 ''、 ''、'10 .100.33.90(S56723FR6VL09) '、'。 '、' 10.00.33.178(S56723FR6VL07) '、'10 .100.33.177 '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' ' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' '、' .100.33.91(S56723FR6VW01) 「」、「 '」、「'」、「 '」、「'」、「 ''、 ''、 ''、 ''、 ''、 ''、 ''、 '、' ']

+0

。 –

+0

以下の前提を設定してください。1:xlrd応答を取得するときにプールごとにアドレスをグループ化する方法はありません。 3:プールのない単一のアドレスが先行され、空の文字列で置き換えられます。 4:応答でアドレスが繰り返されません。これらの仮定は正しいのでしょうか? – BoboDarph

答えて

1
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) 

EDIT:franciscosollimaのソリューション@文

+0

おかげさまで非常に助かりました。私は2日間過ごして、どこにもいなくなった! – degixer

+0

答えは間違っていますか? –

+0

それはまだです.... – degixer

1

のために良いです修正しました。正規表現には別の方法があります。

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'] 
+0

優れたソリューション、非常にきれいな私はそれがcoldspeed – degixer

1
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 
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は、Excelからデータを抽出の一部としてこれを行う必要があります

+0

おかげBoboDarphを.thanks好きな私はちょうどそのリンクを見 – degixer

+0

を与えた基本的に私がしたすべては、(リスト項目に基づいて、サブリストにリストを分割する方法を見つけました。その後、「」)と、カウンタが増加し、各非空のサブリストのためのサブリストを印刷する - そのあなたのケースで空の要素です。そして、(あなたのケースでは、文字列「IPプールのメンバー」である)リストからすべての不要なものをポップ。 – BoboDarph

関連する問題