为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕桂英4014372 2023-10-26 16:31:04
我有 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!
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

使用 defaultdict 的做法非常正确,但是有一种简单的方法可以检查某个键是否在一行中,这就是关键字in。如果您事先知道关键字是什么,那么这就是我将使用的代码:


from collections import defaultdict

def 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,4

2020-09-25,moved,3

2020-09-25,modified,10

2020-09-25,deleted,2

2020-09-30,Created,4

2020-09-30,modified,3

2020-09-30,deleted,3

您还可以执行一些操作,例如定义要打印日期和键的顺序或在循环之前进行排序(如果需要)。


查看完整回答
反对 回复 2023-10-26
  • 1 回答
  • 0 关注
  • 114 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信