Python中的条件加法
我一直在努力使用一个文本文件来使用Python执行有条件的附加/扩展某些文本。如果这太基本,并且已经在这里以某种方式讨论过,我提前表示歉意:( 关于代码(附件),我需要在包含“description”的语句下添加语句“mtu 1546”,如果它不存在的话。另外,我希望能够在接口语句下添加“description TEST”语句(和/或以上的mtu语句,如果有)。我使用的是python 2.7 这是我的代码:Python中的条件加法,python,Python,我一直在努力使用一个文本文件来使用Python执行有条件的附加/扩展某些文本。如果这太基本,并且已经在这里以某种方式讨论过,我提前表示歉意:( 关于代码(附件),我需要在包含“description”的语句下添加语句“mtu 1546”,如果它不存在的话。另外,我希望能够在接口语句下添加“description TEST”语句(和/或以上的mtu语句,如果有)。我使用的是python 2.7 这是我的代码: import re f = open('/TESTFOLDER/TEST.txt','
import re
f = open('/TESTFOLDER/TEST.txt','r')
interfaces=re.findall(r'(^interface Vlan[\d+].*\n.+?\n!)',f.read(),re.DOTALL|re.MULTILINE)
for i in interfaces:
interfaceslist = i.split("!")
for i in interfaceslist:
if "mtu" not in i:
print i
f.close()
print语句可以很好地处理这个条件,因为它能够正确地打印感兴趣的行,但是我的要求是添加(追加/扩展)将所需语句添加到列表中,以便我可以进一步使用它进行解析和填充。在尝试附加/扩展函数时,解释器抱怨它是一个字符串对象。这是示例源文件(文本)。我将解析的文本文件很大,因此只添加了感兴趣的文本
!
interface Vlan2268
description SNMA_Lovetch_mgmt
mtu 1546
no ip address
xconnect 22.93.94.56 2268 encapsulation mpls
!
interface Vlan2269
description SNMA_Targoviste_mgmt
mtu 1546
no ip address
xconnect 22.93.94.90 2269 encapsulation mpls
!
interface Vlan2272
mtu 1546
no ip address
xconnect 22.93.94.72 2272 encapsulation mpls
!
interface Vlan2282
description SNMA_Ruse_mgmt
no ip address
xconnect 22.93.94.38 2282 encapsulation mpls
!
interface Vlan2284
mtu 1546
no ip address
xconnect vfi SNMA_Razgrad_mgmt
!
interface Vlan2286
description mgmt_SNMA_Rs
no ip address
xconnect 22.93.94.86 2286 encapsulation mpls
!
interface Vlan2292
description SNMA_Vraca_mgmt
mtu 1546
no ip address
xconnect 22.93.94.60 2292 encapsulation mpls
!
你的问题的基本答案很简单。字符串是不可变的,因此你不能
附加它们或扩展它们。你必须使用连接创建一个新字符串
>>> print i
interface Vlan2286
description mgmt_SNMA_Rs
no ip address
xconnect 22.93.94.86 2286 encapsulation mpls
>>> print i + ' mtu 1546\n'
interface Vlan2286
description mgmt_SNMA_Rs
no ip address
xconnect 22.93.94.86 2286 encapsulation mpls
mtu 1546
然后您必须将结果保存到变量名或某种容器中。您可以将其保存到我喜欢的位置:
i = i + ' mtu 1546\n'
或者像这样:
i += ' mtu 1546\n'
但在这种情况下,列表理解可能很有用
def add_mtu(i):
return i if "mtu" in i else i + " mtu 1546\n"
for iface in interfaces:
interfaceslist = iface.split("!")
updated_ifaces = [add_mtu(i) for i in interfaceslist]
请注意,为了清晰起见,我将第一个I
替换为iface
。而且,在我看来,interfaces
中现在只有一个iface
。对于循环,您可能需要它,但如果不需要,则可以简化删除它的过程。如果您可以阅读整个文件:
import re
f = open('/TESTFOLDER/TEST.txt','r')
text = f.read()
text = re.sub(r"(?m)(^interface Vlan\d+.*\n(?! description )", r"\1 description TEST\n", text)
text = re.sub(r"(?m)(^interface Vlan\d+.*\n description .+\n)(?! mtu )", r"\1 mtu 1546\n", text)
非常感谢senderle,它看起来棒极了!!我会很快尝试并让你知道..再次感谢..senderle-它与MTU一起工作非常好..关于描述语句和MTU如何。我如何集成代码的这一方面?我必须为此创建不同的函数吗?任何代码都将受到赞赏..我有这样的情况文本文件中可能缺少语句“mtu”或“description”中的一个或两个。再次感谢所有帮助您只需修改当前的add\u mtu
函数即可。如果在…
中出现“mtu”,则不必返回i的结果,而是将其保存为变量名,并对描述执行相同的操作。效果很好:)非常感谢。。非常感谢,真的!!您是否注意到在两个for循环中都使用了变量i?:)事实上,Python知道这是一个不同的变量,但对人类读者来说,这是令人困惑的。谢谢Michael。这也是我的想法……事实上,这是同一个变量。它之所以有效,是因为内环位于外环主体的末端。如果在完成内部循环后在外部循环中使用i
,则其值将是interfaceslist
的最后一个元素。