【发布时间】:2019-12-02 19:07:02
【问题描述】:
输出不是预期的。我希望它在“系统”目录下打印所有关于“系统”的差异,以及在“接口”目录下与“接口”相关的所有差异。 “{ ... }” 也没有打印,即使我有一个声明要抓住它。代码如下:
import re
template_list = ['system','interfaces']
directory = '/templates/juniper/junos/vfirewall/'
diff = """[edit system name-server]
8.8.8.8 { ... }
+ 4.4.4.4;
[edit interfaces ge-0/0/0 unit 0 family inet]
+ address 10.20.30.10/24;
- address 10.50.30.10/24;
[edit interfaces]
+ ge-0/0/1 {
+ unit 2 {
+ family inet {
+ address 10.50.80.10/24;
+ }
+ }
+ }""".splitlines()
for template in template_list:
print("{}{}".format(directory,template))
for line in diff:
if(re.match('\[edit\s({})'.format(template),line)):
print('{}'.format(line))
elif(re.match('\{ ... \}',line)):
print('{}'.format(line))
elif(re.match('^\-',line)):
print('{}'.format(line))
elif(re.match('^\+',line)):
print('{}'.format(line))
elif(re.match('\[edit\s\w.+',line)):
break
输出给出:
/templates/juniper/junos/vfirewall/system
[edit system name-server]
+ 4.4.4.4;
/templates/juniper/junos/vfirewall/interfaces
>>>
预期输出:
/templates/juniper/junos/vfirewall/system
[edit system name-server]
8.8.8.8 { ... }
+ 4.4.4.4;
/templates/juniper/junos/vfirewall/interfaces
[edit interfaces ge-0/0/0 unit 0 family inet]
+ address 10.20.30.10/24;
- address 10.50.30.10/24;
[edit interfaces]
+ ge-0/0/1 {
+ unit 2 {
+ family inet {
+ address 10.50.80.10/24;
+ }
+ }
+ }
【问题讨论】:
-
在创建正则表达式时使用原始字符串。
-
当然,但是当我包含 raw 时它仍然不能解决我的问题。
-
您还需要使用
re.search而不是re.match。 stackoverflow.com/questions/180986/…
标签: python printing logic conditional-statements