Python 3.x AttributeError: NoneType object has no attribute groupdict

2024/11/17 0:35:19

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 refmt = (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")
Answer

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'}
https://en.xdnf.cn/q/120266.html

Related Q&A

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …

why I have negative date by subtraction of two column?

Im trying to create a column his values is the subtraction of two column but I found strange values:Patient["Waiting"] = Patient["Appointment"] - Patient["Scheduled"]Sched…

Python 3: AttributeError: int object has no attribute choice [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Scraping web pages with Python vs PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

error switching to iframe selenium python

Currently Im attempting to switch to iframe/fancybox, but im getting the following error:line 237, in check_response raise exception_class (message, screen, stacktrace) selenium.common.exceptions.WebDr…

SQL Date Variables in Python

I am writing a query inside a Python script.The query is as follows:cur = conn.cursor() query1 = """select max(date_time) from tablename""" cur.execute(queryy1) conn.commi…

Truble to create a matrix for sudoku in python

I have to make a sudoku template, so I need to get random numbers into the matrix, but they can not be repeated in the rows or columns, but I can not do it. For this I can not use numpy. In this case I…

Password validation in python with regex without duplicate chars

The parameter is a string. Check whether it forms a secure password.A password is safe ifthere is at least a lowercase letter in it, and there is at least one capital letter in it, and there is at leas…

Processing non-english text

I have a python file that reads a file given by the user, processes it, and ask questions in flash card format. The program works fine with an english txt file but I encounter errors when trying to pro…