-2

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :

AttributeError: 'NoneType' object has no attribute 'groupdict'

Following is the section of code,re-phrased though,still this does result in the same problem.

import re

fmt = (r"\+((?P<day>\d+)d)?((?P<hrs>\d+)h)?((?P<min>\d+)m)?"
       r"((?P<sec>\d+)s)?((?P<ms>\d+)ms)?$")
p = re.compile(fmt)
match = p.search('Total run time: 9h 34m 9s 901ms realtime, 7h 6m 29s 699ms uptime')
try:
    d = match.groupdict()
except IndexError:
    print("exception here")

4 Answers 4

3

Regular expression functions in Python return None when the regular expression does not match the given string. So in your case, match is None, so calling match.groupdict() is trying to call a method on nothing.

You should check for match first, and then you also don’t need to catch any exception when accessing groupdict():

match = p.search('Total run time: 9h 34m 9s 901ms realtime, 7h 6m 29s 699ms uptime')
if match:
    d = match.groupdict()

In your particular case, the expression cannot match because at the very beginning, it is looking for a + sign. And there is not a single plus sign in your string, so the matching is bound to fail. Also, in the expression, there is no separator beween the various time values.

Try this:

>>> expr = re.compile(r"((?P<day>\d+)d)?\s*((?P<hrs>\d+)h)?\s*((?P<min>\d+)m)?\s*((?P<sec>\d+)s)?\s*(?P<ms>\d+)ms")
>>> match = expr.search('Total run time: 9h 34m 9s 901ms realtime, 7h 6m 29s 699ms uptime')
>>> match.groupdict()
{'sec': '9', 'ms': '901', 'hrs': '9', 'day': None, 'min': '34'}
1
  • thanx, i believe the expression was messed up
    – Naman
    Jun 12, 2015 at 9:26
1

Here, match is evaluating to None, which is NoneType in python. Hence, you are getting the error.

You can put a null check to avoid it, like:

try:
    if !(match is None):
        d = match.groupdict()
1

As re.search can return None if it fails (see), you should check the returned result explicitly before proceeding:

if match is not None:
    d = match.groupdict()
1

Here are the steps to understand the error:

  1. 'NoneType' object has no attribute 'groupdict' means you are calling the method groupdict() on an object containing None. Looking at your code, the variable match contains 'None'.

  2. Where is match initialised ? Just before, with re.search().

  3. Looking at the documentation for regular expression usage in python: https://docs.python.org/3.4/library/re.html#match-objects So re.search() returns a None object, wich is completely normal if the regexp doesn't match your subject string. Then you call a method on this None object without controlling its value.

A solution among other, replace

try:
    d = match.groupdict()
except IndexError:
    print("exception here")

by

if match is not None:
    d = match.groupdict()
else:
    print("re.search() returned None")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.