2017-08-30 51 views
1
變量lenght值追加到一個列表

我有一個包含輸入象下面這樣的文件:如何在Python

host1 192.168.100.24 
[email protected] host2 192.168.100.45 host7 192.168.100.40 host3 192.168.100.34 host4 192.168.100.20 
[email protected] host8 192.168.100.48 host6 192.168.100.43 host10 192.168.100.37 
host5 192.168.100.24 host9 192.168.100.33 

預期輸出:

no_email: 
     host1 192.168.100.24 
     host5 192.168.100.24 
     host9 192.168.100.33 
[email protected]: 
      host2 192.168.100.45 
      host7 192.168.100.40 
      host3 192.168.100.34 
      host4 192.168.100.20 
[email protected]: 
      host8 192.168.100.48 
      host6 192.168.100.43 
      host10 192.168.100.37 

代碼:

def get_contacts(filename): 

emails = [] 
hostname = [] 
ip = [] 
with open(filename,'r') as contacts_file: 
    for a_contact in contacts_file: 
     match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', a_contact.split()[0]) 
     if match == None: 
      emails.append('no_email') 
      hostname.append(a_contact.split()[0]) 
      ip.append(a_contact.split()[1]) 
     line_length = a_contact.count(' ') 
     elif line_length > 1: 
      emails.append(a_contact.split()[0]) 
      hostname.append(a_contact.split()[1]) 
      ip.append(a_contact.split()[2]) 
     else: 
      emails.append(a_contact.split()[0]) 
      hostname.append(a_contact.split()[1]) 
      ip.append(a_contact.split()[2]) 
return emails, hostname, ip 

我只想返回將用於發送到從列表返回的指定電子郵件地址的主機名和IP列表。任何人都可以幫助我輕鬆完成任務嗎?謝謝。

+1

如果您使用'dict'將主機名和ips作爲值存儲爲電子郵件/無電子郵件作爲密鑰,可以嗎? – officialaimm

+0

@officialaimm,是的,它會沒事的。謝謝。 – rmstmg

回答

0

希望這會有所幫助。使用字典的方法是明智的,其中密鑰可以是no_emailemail_id(如果與email-regex匹配)。對於每次迭代,我們首先將to_update變量設置爲no-email,並且僅在找到匹配的電子郵件時才更改它。 host_and_ip變量相應地被設置爲僅用主機和ips來獲取每行的部分,即當檢測到匹配的電子郵件時剝離電子郵件地址。如果檢測到電子郵件,我們會看到相同的電子郵件是否已經在我們的字典dicto中,如果是,我們只需更新主機,並且我們會正確初始化電子郵件列表(作爲新密鑰)。

import re 
def get_contacts(filename): 
    dicto={} 
    dicto['no_email']=[] 
    with open(filename,'r') as contacts_file: 
     for a_contact in contacts_file: 
      match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', a_contact.split()[0]) 
      to_update = 'no_email'  #by default to_update is set to no_email 
      if match == None: 
       host_and_ip = a_contact.split() #grab all as host and ip 
      else: 
       curr_email = a_contact.split()[0] 
       if curr_email not in dicto.keys(): 
        dicto[curr_email]=[] #initialize for new email 
       host_and_ip = a_contact.split()[1:] #grab leaving one behind i.e. the email 
       to_update = curr_email #to be updated to the email 
      for i in range(len(host_and_ip)//2): 
       dicto[to_update]+=[[host_and_ip[2*i],host_and_ip[2*i+1]]] 
    return dicto 

print(get_contacts('test.txt')) 

功能將使字典看起來像這樣:

{'no_email': [['host1', '192.168.100.24'], ['host4', '192.168.100.20'], ['host5', '192.168.100.24'], ['host9', '192.168.100.33']], '[email protected]': [['host2', '192.168.100.45'], ['host7', '192.168.100.40'], ['host3', '192.168.100.34']], '[email protected]': [['host8', '192.168.100.48'], ['host6', '192.168.100.43'], ['host10', '192.168.100.37']]} 

您可以輕鬆地訪問主機和IP列表中特定電子郵件ID簡稱爲:

get_contacts('test.txt')['[email protected]']這將返回返回主機和ips的列表。

2

首先安裝validate_email模塊:

$pip3 install validate_email 

然後:

from validate_email import validate_email 

result = {} 
with open('file.txt') as f: 
    for line in f: 
     words = line.split() 
     if validate_email(words[0]): # If first word of the line is a valid email, lets store data on the result dict using the email as key. 
      email = words[0] 
      words = words[1:] 
     else: 
      email = 'no_email' 

     hosts_emails = [(words[i], words[i+1]) for i in range(0, len(words) - 1, 2)] 
     (result.setdefault(email, [])).append(hosts_emails) 

print(result) 

OUTPUT:

{'no_email': [[('host1', '192.168.100.24')], [('host5', '192.168.100.24'), ('host9', '192.168.100.33')]], '[email protected]': [[('host2', '192.168.100.45'), ('host7', '192.168.100.40'), ('host3', '192.168.100.34'), ('host4', '192.168.100.20')]], '[email protected]': [[('host8', '192.168.100.48'), ('host6', '192.168.100.43'), ('host10', '192.168.100.37')]]} 
+1

驗證電子郵件看起來很有趣。謝謝。 – pylang

0

我使用第三方庫,more_itertools以便於執行grouper itertools配方。這可以通過pip install more_itertools進行安裝。

import more_itertools as mit 


dd = ct.defaultdict(list) 
with open(filename, "r") as f: 
    for line in f.readlines(): 
     parts = line.split() 
     if "@" not in parts[0]: 
      dd["no email"].extend(list(mit.grouper(2, parts))) 
     else: 
      name = parts[0] 
      dd[name].extend(list(mit.grouper(2, parts[1:]))) 

dd 

輸出

defaultdict(list, 
      {'no email': [ 
       ('host1', '192.168.100.24'), 
       ('host5', '192.168.100.24'), 
       ('host9', '192.168.100.33')], 
      '[email protected]': [ 
       ('host2', '192.168.100.45'), 
       ('host7', '192.168.100.40'), 
       ('host3', '192.168.100.34'), 
       ('host4', '192.168.100.20')], 
      '[email protected]': [ 
       ('host8', '192.168.100.48'), 
       ('host6', '192.168.100.43'), 
       ('host10', '192.168.100.37')]}) 

與重新分塊的(主機,IP)一起afters各線已經由空格分開的grouper配方助劑。


您可以選擇實施此配方,而無需安裝more_itertools

itertools recipes(在Python 3):

from itertools import zip_longest 


def grouper(iterable, n, fillvalue=None): 
    "Collect data into fixed-length chunks or blocks" 
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" 
    args = [iter(iterable)] * n 
    return zip_longest(*args, fillvalue=fillvalue) 
0

一種方法是將分割的每一行,並確定是否第一條目中有一個@字符。然後用切片提取剩餘的條目:

def get_contacts(filename): 
    no_email = [] 
    users = [] 

    with open(filename) as f_contacts: 
     for row in f_contacts: 
      entries = row.split() 

      if '@' in entries[0]: 
       pairs = [entries[i:i+2] for i in range(1, len(entries), 2)] 
       users.append([entries[0], pairs]) 
      else: 
       for i in range(0, len(entries), 2): 
        no_email.append(entries[i:i+2]) 

    return no_email, users 

no_email, users = get_contacts('contacts.txt')    

print "no_email:" 
for host, ip in no_email: 
    print " {} {}".format(host, ip) 

for user_entry in users:  
    print "{}:".format(user_entry[0]) 
    for host, ip in user_entry[1]: 
     print " {} {}".format(host, ip) 

這將顯示:

no_email: 
    host1 192.168.100.24 
    host5 192.168.100.24 
    host9 192.168.100.33 
[email protected]: 
    host2 192.168.100.45 
    host7 192.168.100.40 
    host3 192.168.100.34 
    host4 192.168.100.20 
[email protected]: 
    host8 192.168.100.48 
    host6 192.168.100.43 
    host10 192.168.100.37 

users專賣店形式["username", [["host1", "ip1"], ["host2, "ip2"]]]

如果你的文件有多個線,爲相同的用戶的條目,有必要使用defaultdict()來存儲同一位用戶的所有條目。