【发布时间】:2014-07-24 16:00:42
【问题描述】:
我有一个 python 脚本,用于解析日志文件并打印出匹配的错误等。我还想做的是打印找到匹配条目的行号。
我看过一些类似的帖子,例如; Search File And Find Exact Match And Print Line? 但我无法成功应用这些示例。
我的代码是:
from sys import argv
script, filename = argv
with open(filename) as f:
data = f.read().splitlines()
# Process counts and matches of events:
assertmatch = [s for s in data if "Assertion" in s]
assertcount = len(assertmatch)
if assertcount > 0:
print " "
print "ASSERTIONS:"
print '\n'.join([str(myelement) for myelement in assertmatch])
【问题讨论】:
标签: python