我可能会向这里走错路,所以寻求一些帮助。
基本上刚才,我有一个脚本可以打开一个日志文件并选出任何匹配值的行。通过这些行,我然后检查是否找到字典中的键,然后打印该行。 我不想让它打印每一行,只要它匹配一次。
到目前为止,我有这个,但似乎break
只能显示一个匹配
对于此示例,我的syslog
文件为:
PHASE 1
PHASE 2
PHASE 2
代码:
phase_dict = {
"PHASE 1": "Phase 1 completed successfully: ",
"PHASE 2": "Phase 2 completed successfully: ",
}
with FileReadBackwards('syslog', encoding="utf-8") as frb:
for line in frb:
if re.findall('first_match', line):
for key,value in phase_dict.iteritems():
if re.search(key, line):
print value + line.rstrip()
我的输出显示:
Phase 2 completed successfully: PHASE 2
Phase 2 completed successfully: PHASE 2
Phase 1 completed successfully: PHASE 1
我试图让它只显示每个匹配。
预期产出:
Phase 2 completed successfully: PHASE 2
Phase 1 completed successfully: PHASE 1
我不确定我是否会在浏览字典并使用for
循环时走下最佳路线
任何帮助将不胜感激
干杯!
答案 0 :(得分:2)
不知道FileReadBackwards
是什么......
set
看到的阶段。 dict
ionaries支持随机访问,因此不需要迭代它们,更不用说使用正则表达式。seen = set()
with FileReadBackwards('syslog', encoding="utf-8") as frb:
for line in frb:
line = line.strip()
if line not in seen and line in phase_dict:
seen.add(line)
print phase_dict[line] + line