亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號