[英]Need help using Python to parse log file
我正在處理的Python腳本存在問題,該腳本在日志文件中搜索是否出現文本字符串,如果找到將它們打印到另一個文件中,我嘗試使用break,但此過程結束了,最終另一個文件中只有一個條目,如果我不使用break,它將結束語句應用於不包含文本的所有行,基本上,我想以包含所有出現的文本的日志文件結束,如果源日志文件中的任何行都不會出現文本,我只希望將一行打印到新日志文件中,說什么也沒找到,這是我現在正在嘗試的代碼-
with open("/var/log/syslog", "r") as vpnfile:
for line in vpnfile:
if "failed CHAP" in line:
print (line,)
elif "failed CHAP" not in line:
continue
else:
print ("Nadda")
你是這個意思嗎?
found = []
with open("/var/log/syslog", "r") as vpnfile:
for line in vpnfile:
if "failed CHAP" in line:
found.append(line)
# no need to check if "failed CHAP" is not in the line, since you
# already know it's not there from the first test failing
if found:
print(" ".join(found))
else:
print("Nadda")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.