[英]Python read log files and get lines containing specific words
我有日志文件(以 YYMMDD 格式命名),我想创建一个只从文件中获取重要信息的脚本(例如包含 "O:NVS:VOICE" 的行)。 我以前从未使用过 Python,所以请帮忙!
这应该可以让你很好地开始:
infile = r"D:\Documents and Settings\xxxx\Desktop\test_log.txt"
important = []
keep_phrases = ["test",
"important",
"keep me"]
with open(infile) as f:
f = f.readlines()
for line in f:
for phrase in keep_phrases:
if phrase in line:
important.append(line)
break
print(important)
它绝不是完美的,例如没有异常处理或模式匹配,但您可以很容易地将这些添加到其中。 查看正则表达式,这可能比短语匹配更好。 如果您的文件非常大,请逐行读取以避免出现 MemoryError。
输入文件:
This line is super important!
don't need this one...
keep me!
bla bla
not bothered
ALWAYS include this test line
输出:
['This line is super important!\n', 'keep me!\n', 'ALWAYS include this test line']
注意:这是 Python 3.3。
您需要知道如何循环目录中的文件、正则表达式以确保您的日志文件格式与您循环的文件匹配、如何打开文件、如何循环打开文件中的行,以及如何检查其中一行是否包含您要查找的内容。
这里有一些代码可以帮助您入门。
with open("log.log" 'r') as f:
for line in f:
if "O:NVS:VOICE" in line:
print line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.