python - attribute groups does not belong to object -


the following code works expected if declare "line" variable @ beginning of script. ...

s = "jul 15 12:12:51 whitelist logger: 1|999999999999|id:d9faff7c-4016-4343-b494-37028763bb66 submit date:1307130919 done date:1307130919 stat:delivrd err:0|l_vb3_nm_k_p|1373687445|vivnel2|l_vb3_gh_k_p|promo_camp1-bd153424349bc647|1" 

when open file , loop through lines, groups attribute not work. error:attributeerror: 'nonetype' object has no attribute 'groups'

# cat mylast.py import re  f = open('customer.csv')  line in f:        logger_re = re.compile(      "logger: ([^ ]+)\       submit date:(\d+)\       done date:(\d+)\       stat:(.+)\       err:(.+)$")       myvalues = logger_re.search(line).groups()       print myvalues  f.close() 

exception:

# python mylast.py traceback (most recent call last):   file "mylast.py", line 13, in ?     myvalues = logger_re.search(line).groups() attributeerror: 'nonetype' object has no attribute 'groups' 

your regular expression not matching actual file contents.

as such, logger_re.search(line) returns none.

the problem here indented regular expression did not compensate whitespace:

logger_re = re.compile(      "logger: ([^ ]+)\       submit date:(\d+)\       done date:(\d+)\       stat:(.+)\       err:(.+)$") 

note whitespace @ start of line there matters. use separate strings (python join them @ compile time):

logger_re = re.compile(      "logger: ([^ ]+) "      "submit date:(\d+) "      "done date:(\d+) "      "stat:(.+) "      "err:(.+)$") 

Comments