2016-11-07 151 views
1

我有這個日誌文件樣本,我需要計算最近一個月,三個月和一年的條目。以下是日誌文件Python返回過去30天的數據

10/14/2015 10:04:25 AM Following file:<open file 'dirs/tmp/bundle_21241.dat.json', mode 'r' at 0x8b73498> has invalid json which is ignored 
11/15/2015 10:42:53 PM Following file:<open file 'dirs/tmp/bundle_21241.dat.json', mode 'r' at 0xa314498> has invalid json which is ignored 
10/21/2015 10:16:42 AM Following hmac:94e301ff67773de56194165451535ba223cd27588221363290fbfcb96d9d0539 with is already in database so dropping 
11/21/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:11:47+0000+ 12.61 0.430 1686.00 
10/21/2015 10:16:42 AM Following hmac:c35330404902c0b1bb5c6d0718407ea12b25a464433bd1e69152ccc0e0b89c9f with is already in database so dropping 
10/17/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:30:21+0000+ 12.61 0.010 1686.00 
10/11/2015 10:16:42 AM Following hmac:8df71a9f6b6f0a0adb48c052767045f37ec34fce9c002a1c0c5ebc38ed500bf8 with is already in database so dropping 
10/15/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:45:40+0000+ 12.61 0.018 1686.00 
12/21/2015 10:16:42 AM Following hmac:fda9f5756461a8bc2922c55e75a31cf4915e6b0d016ecb786666624a0f04a02f with is already in database so dropping 
12/10/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:01:01+0000+ 12.60 0.048 1686.00 
07/21/2015 10:16:42 AM Following hmac:84d9cdb2145b7c3e0fa2d099070b7bd291c652f30ca25c69240e33ebbd2b8677 with is already in database so dropping 

的只是一些幾行字下面是我的代碼

from datetime import date 
from datetime import time 
from datetime import datetime 
from datetime import timedelta 
import os 

def fileCount(fileName): 

    with open(fileName) as FileObj: 

     Count = 0 
     today_date = date.today() 
     One_Year = str(today_date - timedelta(days=365)) 
     One_Month = str(today_date - timedelta(days=30)) 
     Three_Months = str(today_date - timedelta(days=90)) 

     while True: 

      line = FileObj.readline() 

      record_date = ('-'.join(line[:10].split('/'))).split(" ") 

      if not line: 

       break 

      if "Following hmac" in line: 

       try: 
        convert_date = datetime.strptime(record_date[0], '%m-%d-%Y') 

        #print "Difference is ", todayDate - convert_date.date() 

        #print convert_date.date() 

        date_diff = str(today_date - convert_date.date()) 

        #print dateDiff[:8] 

        if date_diff[:8] < One_Month: 

         Count += 1 

         #print "Last 30 Days Failed HMAC is ", Count 

        else: 

         continue 

       #print convert_date.date() 

       except ValueError: 

        print 'This line has a problem:', record_date 


     print "The Total Number of Failed HMAC is ", Count  

# Call The function 
def main(): 

    filePath = 'file.txt' 

    fileCount(filePath) 

if __name__ == "__main__": 

    main() 

我在編程新手,不理解日期運算良好。目前我正在獲得答案,但他們似乎沒有返回正確的值。我們的目標是遍歷每一行並計算最後30,60和365天間隔的行數。我的代碼目前包含過去30天的測試,但我收到了錯誤的值。

+0

你的變量命名是壞的。 'OneMonth'應該被命名爲'one_month',否則它看起來像一個類名。 –

+0

謝謝。我已根據要求做了必要的修改。 – user1895915

+0

如果您有時間,請閱讀https://www.python.org/dev/peps/pep-0008/ –

回答

0

您需要將所有內容轉換爲日期時間對象以便能夠比較項目。另外,通過在列表中定義它們並使用Python Counter()來對它們進行相應計數,可以更輕鬆地處理所有不同的範圍。這樣可以更容易地擴展範圍。

from datetime import datetime, timedelta 
from collections import Counter 


def fileCount(fileName): 
    log_entry_counts = Counter() 
    today = datetime.today() 

    date_ranges = [ 
     ('three months', today - timedelta(days=90)), 
     ('month', today - timedelta(days=30)), 
     ('year', today - timedelta(days=365))] 

    with open(fileName) as f_input: 
     for line in f_input: 
      if "Following hmac" in line: 
       log_date = datetime.strptime(line[:10], '%m/%d/%Y') 

       for text, dr in date_ranges: 
        if log_date >= dr: 
         log_entry_counts[text] += 1 

    total = 0 

    for text, count in log_entry_counts.items(): 
     print "Failed HMAC in the last {}: {}".format(text, count) 
     total += count 

    print "Total failed HMAC:", total 

fileCount('input.txt') 

這將使你的輸出看起來像:

Failed HMAC in the last three months: 1 
Failed HMAC in the last month: 1 
Failed HMAC in the last year: 2 
Total failed HMAC: 4 
+0

非常感謝。我非常努力地理解這些概念,我知道隨着時間的推移,他們會變得更容易 – user1895915

相關問題