我必须多次运行if查询。现在,我当然可以在代码中多次包含查询了。这是干净的方法吗?还是我应该这样写?
这是我的查询
if 'xyc_begin' in content and 'xyc_end' in content:
file.write('xyc available\n')
else:
print('xyc not available\n')
file.write('xyc not available\n')
file.close()
sys.exit()
现在,我将不得不检查abc和def(而不是xyc)并相应地调整文件中的行。您如何正确地对此进行编程,或者只是执行几个查询?
放入列表中:
content = "aaa xyc_begin xyc_end def_begin def_end bfasfsdnfl"
look_out_for = ["xyc","abc","def"]
with open("f.txt","w") as f:
for what in look_out_for:
if f"{what}_begin" in content and f"{what}_end" in content:
f.write(f"{what} available\n")
else:
f.write(f"{what} NOT available\n")
print(open("f.txt").read())
档案内容:
xyc available
abc NOT available
def available
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句