2013-04-26 53 views
0
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#fixed the import, just red PEP 8 
import re 
################################################### 
def extract(data): 
    ms = re.match(r'(\S+).*mid:(\d+)' , data) # heure & mid 
    k = re.findall(r"/(\S+)", data) # source & destination 
    exp = result = re.findall(r'NVS:([\w\.]+)',data) # type S & D 
    # if the table length is 1 it means that the origin is unknown 
    if len(k)==1: 
     return {'Heure':ms.group(1), 'mid':ms.group(2),"Origine":"Unknown","Destination":k[0],"Type S":exp[0],"Type D":exp[1]} 
    # if the table length it means that there's a a source and a destination 
    if len(k)==2: 
     return {'Heure':ms.group(1), 'mid':ms.group(2),"Origine":k[0],"Destination":k[1],"Type S":exp[0],"Type D":exp[1]} 

很好的问题是,当我有行一样,那里的[NVS:FAXG3.1.0/+44614215421]返回None,有没有办法让被停止在第二NVS:使其stopps一样,如果我们有像data2正则表达式的Python解析文件

data = "13:16:16.146 mta   Messages  I CC Doc O:NVS:SMTP/[email protected] R:NVS:SMTP.0/[email protected] [NVS:FAXG3.1.0/+44614215421] mid:41414" 
print extract(data) 

一行它返回

>>>None 

data2 = "13:16:16.146 mta   Messages  I CC Doc O:NVS:SMTP/[email protected] R:NVS:SMTP.0/[email protected] mid:41414" 
print extract(data2) 

它返回


>>> {'Destination': '[email protected]', 'mid': '41414', 'Type S': 'SMTP', 'Origine': '[email protected]', 'Type D': 'SMTP.0', 'Heure': '13:16:16.146'} 

回答

1

肮脏的黑客

只需更换

if len(k)==2: 

if len(k)>1: 

,因为它发现第二个字符串中有多于2个匹配项