【问题标题】:AttributeError: 'NoneType' object has no attribute 'group' , group doesn't return noneAttributeError:“NoneType”对象没有属性“组”,组不返回无
【发布时间】:2021-09-27 19:37:45
【问题描述】:

我遇到了正则表达式的问题,我查看了许多类似的问题,发现提到的答案与 None 或 retuning none 上的组有关。


import re
route={}
gig_pattern = re.compile('(GigabitEthernet)([0-9]\/[0-9]\/[0-9]\/[0-9])')
with open('ip-routes.txt','r')as f:
 for line in f:
   match = gig_pattern.search( line ) # Match for Gigabit Ethernet
   intf=match.group(2)
   route[inft]=route[intf]+1 if intf in route else 1 

但是如果我打印变量(匹配),我可以看到正则表达式的结果,这意味着它没有返回非,而是返回预期的模式

【问题讨论】:

标签: regex


【解决方案1】:

问题已通过修改如下代码得到解决


import re
route={}

gig_pattern = re.compile('(GigabitEthernet)([0-9]\/[0-9]\/[0-9]\/[0-9])')
with open('ip-routes.txt','r')as f:
 for line in f:
   match = gig_pattern.search( line ) # Match for Gigabit Ethernet
   if match is not None:
    intf = match.group(2)
    route[intf]=route[intf]+1 if intf in route else 1 
   


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多