第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 python 中的日志文件中的日期和計(jì)數(shù)匹配單詞進(jìn)行分組

使用 python 中的日志文件中的日期和計(jì)數(shù)匹配單詞進(jìn)行分組

慕桂英4014372 2023-10-26 16:31:04
我有 myFileMonitor.log 文件,其中包含以下數(shù)據(jù)。我想根據(jù)日期分組和匹配單詞(如 - 'Created', 'modified', 'moved', 'deleted')將數(shù)據(jù)保存在 csv 文件中,如下所示。因此,在日志文件中,我想根據(jù)日期過濾數(shù)據(jù),并計(jì)算這些單詞在日志中出現(xiàn)的次數(shù)。請(qǐng)協(xié)助。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函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用 defaultdict 的做法非常正確,但是有一種簡單的方法可以檢查某個(gè)鍵是否在一行中,這就是關(guān)鍵字in。如果您事先知道關(guān)鍵字是什么,那么這就是我將使用的代碼:


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

您還可以執(zhí)行一些操作,例如定義要打印日期和鍵的順序或在循環(huán)之前進(jìn)行排序(如果需要)。


查看完整回答
反對(duì) 回復(fù) 2023-10-26
  • 1 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)