我有一个表(在本例中是字符串),如下所示:
Community Group / Access context acl_filter
--------- -------------- ------- ----------
Community_test-1 network-operator C0n_text! T3st-ACL#$
WEWORK network-operator
RW network-admin _C0n
YANKS network-admin my_acl我必须使用单个regex来匹配和解析所有值,而不需要使用额外的代码,但是编写正则表达式匹配缺少的值也有问题。
现在,让我们忽略前两行,关注实际值:
import re
import pprint
show_snmp_community = """
Community_test-1 network-operator C0n_text! T3st-ACL#$
WEWORK network-operator
RW network-admin _C0n
YANKS network-admin my_acl"""
show_snmp_community_regex = "(?P<community>\S+)\s+(?P<group_access>\S+)\s+(?P<context>\S+)\s+(?P<acl>\S+)"
show_snmp_community_split = show_snmp_community.split('\n')
final_dict = {}
i = 0
for line in show_snmp_community_split:
snmp_dict = {}
match_snmp = re.match(show_snmp_community_regex, line)
group_snmp = match_snmp.groupdict()
community = group_snmp["community"]
snmp_dict["community"] = community
group_access = group_snmp["group_access"]
snmp_dict["group_access"] = group_access
context = group_snmp["context"]
snmp_dict["context"] = context
acl = group_snmp["context"]
snmp_dict["acl"] = acl
final_dict[i] = snmp_dict
i += 1
pretty = pprint.PrettyPrinter(indent=2, depth=10).pprint
pretty(final_dict)显然,这导致:
Traceback (most recent call last):
File "C:\Users\Gabriele\Desktop\prova.py", line 18, in <module>
group_snmp = match_snmp.groupdict()
AttributeError: 'NoneType' object has no attribute 'groupdict'这就是我想要达到的目标:
{ 0: { 'acl': 'C0n_text!',
'community': 'Community_test-1',
'context': 'C0n_text!',
'group_access': 'network-operator'}
1: { 'acl': '',
'community': 'WEWORK',
'context': '',
'group_access': 'network-operator'}
2: { 'acl': '',
'community': 'RW',
'context': '_C0n',
'group_access': 'network-admin'}
3: { 'acl': 'my_acl',
'community': 'YANKS',
'context': '',
'group_access': 'network-admin'}}我用不同的正则表达式尝试过许多东西,但没有成功:
发布于 2015-10-29 09:01:34
引发此错误是因为,当您将原始字符串拆分为show_snmp_community_split时,第一个元素是一个空字符串:''。与re.match一起使用时,这将返回None。
在循环中使用if-语句:
for line in show_snmp_community_split:
snmp_dict = {}
match_snmp = re.match(show_snmp_community_regex, line)
if not match_snmp:
continue
group_snmp = match_snmp.groupdict()
community = group_snmp["community"]
snmp_dict["community"] = community
group_access = group_snmp["group_access"]
snmp_dict["group_access"] = group_access
context = group_snmp["context"]
snmp_dict["context"] = context
acl = group_snmp["context"]
snmp_dict["acl"] = acl
final_dict[i] = snmp_dict
i += 1接下来,由于acl和context可能会出现空列,所以模式中的\S+匹配将失败。更新模式如下:
show_snmp_community_regex = "^(?P<community>\S+)\s+(?P<group_access>\S+)\s*(?P<context>\S*)\s*(?P<acl>\S*)$"在本地测试时的输出:
┌─[hjpotter92]─[Oct 29, 2015]─[Programming]
└──$ temp.py
{ 0: { 'acl': 'C0n_text!',
'community': 'Community_test-1',
'context': 'C0n_text!',
'group_access': 'network-operator'},
1: { 'acl': '',
'community': 'WEWORK',
'context': '',
'group_access': 'network-operator'},
2: { 'acl': '_C0n',
'community': 'RW',
'context': '_C0n',
'group_access': 'network-admin'},
3: { 'acl': 'my_acl',
'community': 'YANKS',
'context': 'my_acl',
'group_access': 'network-admin'}}https://stackoverflow.com/questions/33409453
复制相似问题