我在这里的脚本应该以这种格式返回结果
[ {'heure':xxxx,'mid': xxxx,'type message': "e.g SMS.Message ", "Origine":xxx,"Destination":xxxx}]
很好,但是没有 Type 消息,我刚刚添加了这个,所以我认为正则表达式不正确。:/ 当我添加一个没有看起来像正则表达式的数据的数据时,它也不起作用,所以我想我必须做一个try:
-except:
但我不知道怎么做。:/
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
################################_Function EXTRACT_###############################################
def extraire(data):
ms = re.match(r'(\S+).*mid:(\d+).*(R:NVS:\w+)', data) # heure & mid
k = re.findall(r"/\S+", data ) # source & destination extracte
return {'Heure':ms.group(1), 'mid':ms.group(2),'Type Message':ms.group(3),"Origine":k[0],"Destination":k[1]}
#################################################################################################
tableau = []
data3 = "12:07:32.546 mta Messages I Doc O:TCARVAL (NVS:SMTP/jack.reacher@example.de) R:NVS:VOICE/+45154245 mid:6500"
data4 = "12:07:41.391 mta Messages I Rep O:TCARVAL (NVS:SMTP/brad.alison@yow.en) R:NVS:**SMS.Message**/+39872422 mid:6500"
data5 = "12:07:32.546 mta Messages I Doc O:TCARVAL (NVS:VOICE/+69517412carval@ifremer.no) R:NVS:SMS.Message/+34659879 mid:6500"
data6 = "12:07:32.545 mta Messages I Doc O:TCARVAL Example@whitout-slash.com R:NVS:VOICE/01020150405 mid:9797"
data_list = [ data3, data4,data5, data6]
tableau = [extraire(data) for data in data_list]
print tableau