Python 使用正则表达式的 .groups() 函数时 NoneType 的正确尝试异常是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19595296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the correct Try Exception for NoneType when using regex's .groups() function
提问by Ryflex
I'm trying to use the following code:
我正在尝试使用以下代码:
try:
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
except TypeError:
clean = ""
However I get the following traceback...
但是我得到以下回溯......
Traceback (most recent call last):
File "test.py", line 116, in <module>
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
AttributeError: 'NoneType' object has no attribute 'groups'
What is the correct exception / correct way around this problem?
解决此问题的正确异常/正确方法是什么?
采纳答案by karthikr
re.match
returns None
if it cannot find a match. Probably the cleanest solution to this problem is to just do this:
re.match
None
如果找不到匹配项,则返回。这个问题最干净的解决方案可能就是这样做:
# There is no need for the try/except anymore
match = re.match(r'^(\S+) (.*?) (\S+)$', full)
if match is not None:
clean = filter(None, match.groups())
else:
clean = ""
Note that you could also do if match:
, but I personally like to do if match is not None:
because it is clearer. "Explicit is better than implicit" remember. ;)
请注意,您也可以这样做if match:
,但我个人喜欢这样做,if match is not None:
因为它更清晰。“显式胜于隐式”记住。;)
回答by karthikr
Try this:
尝试这个:
clean = ""
regex = re.match(r'^(\S+) (.*?) (\S+)$', full)
if regex:
clean = filter(None, regex.groups())
The problem is, re.match(r'^(\S+) (.*?) (\S+)$', full)
returns a None
if it does not find a match. Hence the error.
问题是,如果找不到匹配项,则re.match(r'^(\S+) (.*?) (\S+)$', full)
返回 a None
。因此错误。
Note: You do not require a try..except
if you handle it this way.
注意:try..except
如果您以这种方式处理,则不需要 a 。
回答by Paco
Traceback (most recent call last):
File "test.py", line 116, in <module>
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
AttributeError: 'NoneType' object has no attribute 'groups'
It tells you what error is to handle: AttributeError
它告诉你要处理什么错误:AttributeError
The, it is either:
,它是:
try:
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
except AttributeError:
clean = ""
or
或者
my_match = re.match(r'^(\S+) (.*?) (\S+)$', full)
my_match = ''
if my_match is not None:
clean = my_match.groups()
回答by Pramit
I think handling AttributeError using try, catch is a much cleaner solution. It may so happen that any transformation on performed the row may be a very expensive operation. For e.g.
我认为使用 try, catch 处理 AttributeError 是一个更简洁的解决方案。可能会发生对行执行的任何转换可能是非常昂贵的操作。例如
match = re.match(r'^(\S+) (.*?) (\S+)$', full)
if match is not None: clean = filter(None, match.groups()) else: clean = ""
如果匹配不是无:clean = filter(None, match.groups()) else:clean = ""
in the above case, the structure of the row changed in such a way that the regex delivered partial result. So, now match will not be none and the AttributeError exception will be thrown.
在上面的例子中,行的结构发生了变化,正则表达式传递了部分结果。所以,现在 match 不会是 none 并且 AttributeError 异常将被抛出。
回答by Bruno
You need to add the AttributeError
to your exception clause.
您需要将AttributeError
加到您的例外条款中。
An except clause may name multiple exceptions as a parenthesized tuple:
一个except子句可以将多个异常命名为一个带括号的元组:
try:
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
except (TypeError, AttributeError):
clean = ""