我想基于if
-else
结果创建结果字符串列表,但是当前在运行循环时只能看到1值字符串。
示例:
for ips in npat:
ipnet = ips.strip()
print ("Processing ..... ", ipnet)
fgen = "grep " +ipnet+ " /mnt/hgfs/IRR/fgen.txt"
f2pat = re.findall(ipnet,fgen)
print ("\nCommand: ",fgen)
os.system(fgen)
print ("\n NEW NPATH: ",f2pat)
flist = []
if ipnet in f2pat:
flist.append("Grep Found")
print ("Result ", flist)
else:
flist.append("Grep NotFound")
print ("Result: ",flist)
flist -> ['Grep Found']
...尽管列表中只有1个值,但它们应该是多个值。我可以知道你的想法吗?
谢谢。
答案 0 :(得分:4)
似乎flist = []
在循环中重新初始化了。仅将该变量移到for loop
上方。因此,代码变为:
flist = []
for ips in npat:
ipnet = ips.strip()
print ("Processing ..... ", ipnet)
fgen = "grep " +ipnet+ " /mnt/hgfs/IRR/fgen.txt"
f2pat = re.findall(ipnet,fgen)
print ("\nCommand: ",fgen)
os.system(fgen)
print ("\n NEW NPATH: ",f2pat)
if ipnet in f2pat:
flist.append("Grep Found")
print ("Result ", flist)
else:
flist.append("Grep NotFound")
print ("Result: ",flist)
贷记给吉德人comment