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'}