python - My script returns an error when the test isn't corresponding to the Regex: 'NoneType' object has no attribute 'group' -


my script down here supposed return result in format

[ {'heure':xxxx,'mid': xxxx,'type message': "e.g sms.message ", "origine":xxx,"destination":xxxx}]  

well works without type message i've added think regex isn't correct. :/ doesn't work when add data doesn't have looks regex think have try: - except: don't know how. :/

#!/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       doc o:tcarval (nvs:smtp/jack.reacher@example.de) r:nvs:voice/+45154245 mid:6500" data4 = "12:07:41.391 mta         messages       rep o:tcarval (nvs:smtp/brad.alison@yow.en) r:nvs:**sms.message**/+39872422 mid:6500" data5 = "12:07:32.546 mta         messages       doc o:tcarval (nvs:voice/+69517412carval@ifremer.no) r:nvs:sms.message/+34659879 mid:6500" data6 = "12:07:32.545 mta     messages   doc o:tcarval example@whitout-slash.com        r:nvs:voice/01020150405 mid:9797" data_list = [ data3, data4,data5, data6] tableau = [extraire(data) data in data_list] print tableau  

change extraire function this, trying access properties on ms when there no matches. , when there no matches, ms none :

def extraire(data):     ms = re.match(r'(\s+).*mid:(\d+).*(r:nvs:\w+)', data) # heure & mid                                      print(str(ms))     if(ms not none):         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],"desti\ nation":k[1]}     else:         return {} 

btw, regex not seem match text intend match. may list index out of range error if k not contain number of elements seeking.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -