我想在“config vdom”字符串的第3场比赛后追加线条。不幸的是,剧本只考虑第一场比赛。
原始代码:
x = list()
def z():
with open('test.conf', 'r') as rf:
for line in rf:
if 'config vdom\n' in line:
while True:
line = (rf.__next__())
if 'end\n' in line:
break
x.append(line)
with open('test.txt', 'w') as wf:
wf.writelines(x)
return
我打算使用枚举,但不确定如何在添加下一行时应用它。
枚举行:
z = [i for i, n in enumerate(y) if n == 'config vdom\n'][2]
print('Line', z, ':', y[z])
输出:
Line 10310 : config vdom
示例数据:
config vdom
config system global
set admin-maintainer disable
set admin-scp enable
end
config vdom*
config system accprofile
edit "prof_admin"
set admingrp read-write
set utmgrp read-write
set vpngrp read-write
set wanoptgrp read-write
set wifi read-write
config vdom*
test sample data
end
预期输出:应该在x列表中添加以下三行。
config vdom*
test sample data
end
答案 0 :(得分:0)
这应该有所帮助。
with open('test.txt', 'a') as wf:
with open('test.conf', 'r') as rf:
c = 0 #Checkvalue
for line in rf:
if 'config vdom\n' in line:
c += 1
if c == 3: #Check if 3rd "config vdom"
while True:
line = next(rf)
if 'end\n' in line:
break
wf.writelines(line) #Write required content.
答案 1 :(得分:0)
itertools.groupby
可以提供帮助:
from itertools import groupby
from io import StringIO
text = '''config vdom
config system global
set admin-maintainer disable
set admin-scp enable
end
config vdom*
config system accprofile
edit "prof_admin"
set admingrp read-write
set utmgrp read-write
set vpngrp read-write
set wanoptgrp read-write
set wifi read-write
config vdom*
test sample data
end'''
count = 0
with StringIO(text) as file:
for key, group in groupby(file, key=lambda x: 'config vdom' in x):
if key is True:
count += 1
if count == 3 and key is False:
with StringIO() as out_file:
out_file.writelines(group)
print(out_file.getvalue())
打印(或写入您的文件)
test sample data
end
...是您想要的输出吗?
对于key
出现的每一行, True
将为'config vdom'
,对于所有其他行,
group
将为key
。 StringIO
会收集所有行,直到sudo port install libsdl2 libsdl2_image libsdl2_ttf libsdl2_mixer
的值发生变化。
您需要将所有$(document).ready(function(){
$(".kl > #nav__ul1-show").click(function(){
$(this).find(".nav__ul1").show();
});
});
部分替换为所需的文件。