Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python脚本从日志文件中查找关键字的类别?_Python - Fatal编程技术网

如何使用python脚本从日志文件中查找关键字的类别?

如何使用python脚本从日志文件中查找关键字的类别?,python,Python,我的输入文件 INCIDENT 677700 password reset INCIDENT 677742 C:\ drive full INCIDENT 500901 mouse not working INCIDENT 500942 unable to connect oracle box INCIDENT 500949 high cpu utilization INCIDENT 600901 sql server clustering failed INCIDENT 490203 Low

我的输入文件

INCIDENT 677700 password reset
INCIDENT 677742 C:\ drive full
INCIDENT 500901 mouse not working
INCIDENT 500942 unable to connect oracle box
INCIDENT 500949 high cpu utilization
INCIDENT 600901 sql server clustering failed
INCIDENT 490203 Low disk space issue
INCIDENT 10I891 Lotus Notes client failed
INCIDENT 489011 Low disk space issue
INCIDENT 89G901 SSIS Load failed

words =["password","drive full","disk space","SSIS","sql server","cpu utilization","oracle","Lotus Notes","mouse"]
我想在我的输出文件中指定类别应该是:以及如何添加将来的单词

Password,INCIDENT 677700 password reset
drive full,INCIDENT 677742 C:\ drive full
mouse,INCIDENT 500901 mouse not working
Oracle,500942 unable to connect oracle box
cpu utilization,INCIDENT 500949 high cpu utilization
sql server,INCIDENT 600901 sql server clustering failed
disk space, INCIDENT 490203 Low disk space issue
Lotus Notes,INCIDENT 10I891 Lotus Notes client failed
disk space,INCIDENT 489011 Low disk space issue
SSIS,INCIDENT 89G901 SSIS Load failed

只需迭代每一行和每一个单词,如果该行中存在单词,则将新行
word,line
写入输出文件

一些假设:

  • 您希望执行无大小写匹配,例如
    磁盘空间
    磁盘空间
    将匹配
  • 日志文件中的每个条目只有一个匹配字。如果有更多条目,那么该行将使用每个单独的条目写入两次
演示:

words = [
    "password",
    "drive full",
    "disk space",
    "SSIS",
    "sql server",
    "cpu utilization",
    "oracle",
    "Lotus Notes",
    "mouse",
]

with open("input.txt") as file, open("output.txt", "w") as out:
    for line in file:
        for word in words:
            # Do lowercase matching
            if word.lower() in line.lower():
                out.write("%s,%s" % (word, line))
output.txt:

还可以使用以下内容压缩两个嵌套循环:


那么,你试过什么?如果一个条目中有两个关键字呢?
password,INCIDENT 677700 password reset
drive full,INCIDENT 677742 C:\ drive full
mouse,INCIDENT 500901 mouse not working
oracle,INCIDENT 500942 unable to connect oracle box
cpu utilization,INCIDENT 500949 high cpu utilization
sql server,INCIDENT 600901 sql server clustering failed
disk space,INCIDENT 490203 Low disk space issue
Lotus Notes,INCIDENT 10I891 Lotus Notes client failed
disk space,INCIDENT 489011 Low disk space issue
SSIS,INCIDENT 89G901 SSIS Load failed
from itertools import product

with open("input.txt") as file, open("output.txt", "w") as out:
    for line, word in product(file, words):
        if word.lower() in line.lower():
            out.write("%s,%s" % (word, line))