使用 python 中的日志文件中的日期和计数匹配单词进行分组

我有 myFileMonitor.log 文件,其中包含以下数据。我想根据日期分组和匹配单词(如 - 'Created', 'modified', 'moved', 'deleted')将数据保存在 csv 文件中,如下所示。因此,在日志文件中,我想根据日期过滤数据,并计算这些单词在日志中出现的次数。请协助。


myFileMonitor.log:

2020-09-25 16:31:58 - Security Alert! ' C:/Users/khond/Downloads/New folder ' has been Created!!

2020-09-25 16:32:11 - Security Alert! Files/Folder moved ' C:/Users/khond/Downloads/New folder ' to ' C:/Users/khond/Downloads/Test1 '

2020-09-25 16:32:12 - Security Alert! ' C:/Users/khond/Downloads/Test1 - Copy ' has been Created!!

2020-09-25 16:32:13 - Security Alert! ' C:/Users/khond/Downloads/Test1 - Copy ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/Code.png ' has been Created!!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/Code.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/Code.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/Code.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/Code.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/PsedoCode.png ' has been Created!!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/PsedoCode.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/PsedoCode.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/PsedoCode.png ' has been modified!

2020-09-25 16:32:30 - Security Alert! ' C:/Users/khond/Downloads/PsedoCode.png ' has been modified!

2020-09-25 16:33:56 - Security Alert! Files/Folder deleted: C:/Users/khond/Downloads/Test1!

2020-09-25 16:34:04 - Security Alert! Files/Folder moved ' C:/Users/khond/Downloads/Test1 - Copy ' to ' C:/Users/khond/Downloads/Test1 '

2020-09-25 16:34:05 - Security Alert! Files/Folder deleted: C:/Users/khond/Downloads/Code.png!



慕桂英4014372
浏览 115回答 1
1回答

HUH函数

使用 defaultdict 的做法非常正确,但是有一种简单的方法可以检查某个键是否在一行中,这就是关键字in。如果您事先知道关键字是什么,那么这就是我将使用的代码:from collections import defaultdictdef collect_data():    try:        occurrences = defaultdict(lambda: defaultdict(int))        keys = {'Created', 'modified', 'deleted', 'moved'}        with open('myFileMonitor.log', 'r') as f:            for line in f:                date = line.split(' ')[0]                for key in keys:                    if key in line:                        occurrences[date][key] += 1        for date in occurrences:            for key in occurrences[date]:                print(date+','+key+','+str(occurrences[date][key]))    except FileNotFoundError:        print("Exception error: File not found!")输出:2020-09-25,Created,42020-09-25,moved,32020-09-25,modified,102020-09-25,deleted,22020-09-30,Created,42020-09-30,modified,32020-09-30,deleted,3您还可以执行一些操作,例如定义要打印日期和键的顺序或在循环之前进行排序(如果需要)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python