打破循环有问题。我想在它到达结束语时打破它。希望你能帮助我在这段代码中缺少什么。谢谢。
def y():
with open('test.conf', 'r') as rf:
x = list()
for line in rf:
if 'config system global' in line:
x.append('config system global\n')
while True:
x.append(rf.__next__())
if 'end\n' in line:
break
with open('test.txt', 'w') as wf:
wf.writelines(x)
config system global
set admin-maintainer disable
set admin-scp enable
set admin-server-cert "Fortinet_Firmware"
set admintimeout 15
set anti-replay disable
set fgd-alert-subscription advisory latest-threat
set gui-dynamic-routing enable
set gui-multiple-utm-profiles enable
set gui-replacement-message-groups enable
set gui-sslvpn-personal-bookmarks enable
set gui-sslvpn-realms enable
set gui-wireless-opensecurity enable
set hostname "XXXXXX"
set internal-switch-mode interface
set revision-backup-on-logout enable
set revision-image-auto-backup enable
set strong-crypto enable
set tcp-timewait-timer 120
set timezone 80
set vdom-admin enable
end
config system accprofile
edit "prof_admin"
set admingrp read-write
set authgrp read-write
set endpoint-control-grp read-write
set fwgrp read-write
set loggrp read-write
set mntgrp read-write
set netgrp read-write
set routegrp read-write
set sysgrp read-write
set updategrp read-write
set utmgrp read-write
set vpngrp read-write
set wanoptgrp read-write
set wifi read-write
答案 0 :(得分:0)
尝试使用re module
。
<强>实施例强>
import re
for line in s.split("\n"):
if re.match('end', line):
break
答案 1 :(得分:0)
问题是内部SELECT cr.CarID, cr.Brand, cl.ColorName, cr.Amount FROM Cars AS cr
LEFT JOIN colors AS cl ON cr.ColorId = cl.ColorId and
cl.colorId is not null
循环:
while
这是一个无限循环,因为while True:
x.append(rf.__next__())
if 'end\n' in line:
break
在循环中不会改变。它可能以line
异常结束,除非它被捕获
正确的代码是:
StopIteration
在while True:
line = rf.__next__()
if 'end\n' in line:
break
x.append(line)
的测试之前或之后添加,具体取决于您是否要将end
保留在块中。
和de-indent:
end
与其他with open('test.txt', 'w') as wf:
wf.writelines(x)
块处于同一级别,或者文件将在每次迭代时写入。