Python 3。x AttributeError: 'NoneType'对象没有属性'groupdict'

[英]Python 3.x AttributeError: 'NoneType' object has no attribute 'groupdict'


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 :

作为一个python初学者,我可能会错过一些基本的东西。但是我正在经历一个项目的代码,碰巧遇到了这个问题:

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

属性错误:'NoneType'对象没有属性'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 个解决方案

#1


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.

当正则表达式与给定字符串不匹配时,Python中的正则表达式函数不会返回。所以在你的例子中,match是None,所以调用match.groupdict()试图调用一个无任何方法的方法。

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

您应该首先检查匹配,然后在访问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'}

#2


0  

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

在这里,match正在对None进行评估,这是python中的NoneType。因此,您将得到错误。

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

您可以使用空检查来避免它,比如:

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

#3


0  

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

当re.search返回None时,如果它失败(见),您应该在继续之前显式地检查返回的结果:

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

#4


0  

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'.

    “NoneType”对象没有属性“groupdict”,这意味着您正在调用一个包含None的对象的方法groupdict()。查看您的代码,变量match包含“None”。

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

    匹配初始化在哪里?之前,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.

    查看python中正则表达式用法的文档:https://docs.python.org/3.4/library/re.html#match-objects。search()返回一个None对象,如果regexp与您的主题字符串不匹配,则完全正常。然后,在这个对象上调用一个方法,而不控制它的值。

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")

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/06/12/5356a3f201f1e5b9d808131a9e67d211.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号