【问题标题】:Export data from log file between time range in Python在 Python 中的时间范围之间从日志文件中导出数据
【发布时间】:2021-01-29 10:42:37
【问题描述】:

我一直在尝试遵循这篇文章中的指导方针,并进行了一些调整: The best way to filter a log by a dates range in python

我的日志文件是这样的:

2020-Oct-12 13:38:57.742759 -0700 : (some text)
(line of text)
(line of text)
2020-Oct-12 13:38:57.742760 -0700 : (some text)
...
2020-Oct-12 13:57:57.742759 -0700 : (some text)

我尝试了这两个代码 sn-ps 但它们没有给出任何东西。日期时间定义有问题吗?

myfile = open('DIR_extract.log', 'w')
with open('DIRDriver.log','r') as f:
    for line in f:
        d = line.split(" ",1)[0] 
        if d >= '2020-10-12 13:38:57' and d <= '2020-10-12 13:57:57':
            myfile.write("%s\n" % line)

还有

myfile = open('DIR_extract.log', 'w')
from itertools import dropwhile, takewhile
from_dt, to_td = '2020-10-12 13:38:57', '2020-10-12 13:57:57'
with open('DIRDriver.log') as fin:
    of_interest = takewhile(lambda L: L <= to_td, dropwhile(lambda L: L < from_dt, fin))
    for line in of_interest:
        myfile.write("%s\n" % line)

【问题讨论】:

    标签: python parsing logging


    【解决方案1】:

    你快到了。

    d = line.split(" ",1)[0] 只返回日期时间的第一部分,例如:2020-Oct-12。 那是因为您的 datetime 格式与您链接到的答案不同。 datetime 之间有空格。

    因此,要使其正常工作,您需要掌握该行的所有日期和时间部分。

    dt_start = '2020-Oct-12 13:38:57'
    dt_end = '2020-Oct-12 13:57:57'
    str_time_len = len(dt_start)
    
    with open('DIR_extract.log', 'w+') as myfile:
        with open('DIRDriver.log','r') as f:
            for line in f:
                date_time = line[:str_time_len]
                if dt_start <= date_time <= dt_end:
                    myfile.write(line)
    

    假设日志文件内容为

    2020-Oct-12 13:35:57.742759 -0700 : before
    2020-Oct-12 13:38:57.742759 -0700 : start
    2020-Oct-12 13:54:57.742759 -0700 : inside
    2020-Oct-12 13:57:57.742759 -0700 : end
    2020-Oct-12 13:59:57.742759 -0700 : outside
    

    上面的代码给出了

    2020-Oct-12 13:38:57.742759 -0700 : start
    2020-Oct-12 13:54:57.742759 -0700 : inside
    2020-Oct-12 13:57:57.742759 -0700 : end
    

    请注意,由于您使用MMM 格式数月,因此上述代码仅适用于一个月内的日志。从Jan 过滤到Apr 或类似的东西不起作用,因为Jan > Apr。您需要将这些字符串转换为 datetime 对象。

    另外,如果一些日志记录是多行的,你需要掌握所有的行,而不仅仅是以datetime开头的行。

    import re
    from datetime import datetime
    
    _start = '2020-Oct-12 13:38:57'
    _end = '2020-Oct-12 13:57:57'
    
    
    dt_fmt = '%Y-%b-%d %H:%M:%S'
    dt_reg = r'\d{4}-[A-Za-z]{3}-\d{2}'
    dt_start = datetime.strptime(_start, dt_fmt)
    dt_end = datetime.strptime(_end, dt_fmt)
    
    str_time_len = len(_start)
    
    with open('DIR_extract.log', 'w+') as myfile:
        with open('DIRDriver.log','r') as f:
            started = False
            for line in f:
                if re.match(dt_reg, line):
                    datetime_str = line[:str_time_len]
                    dt = datetime.strptime(datetime_str, dt_fmt)
                    if not started and dt >= dt_start:
                        started = True
                    elif started and dt > dt_end:
                        break
    
                if not started:
                    continue
    
                myfile.write(line)
                print(line.strip())
    

    假设日志文件内容如下:

    2020-Oct-12 13:35:57.742759 -0700 : before
    2020-Oct-12 13:38:57.742759 -0700 : start
    2020-Oct-12 13:54:57.742759 -0700 : inside
    (line of text)
    (line of text)
    2020-Oct-12 13:57:57.742759 -0700 : end
    2020-Oct-12 13:59:57.742759 -0700 : outside
    

    它给你:

    2020-Oct-12 13:38:57.742759 -0700 : start
    2020-Oct-12 13:54:57.742759 -0700 : inside
    (line of text)
    (line of text)
    2020-Oct-12 13:57:57.742759 -0700 : end
    

    【讨论】:

    • 这几乎行得通!实际上,它是有效的,只是我没有意识到我的情况有点微妙。我已经编辑了这个问题。有时,在两个日期时间行之间,有一些没有日期的文本行。我需要包括这两个,因为它属于更广泛的日期时间范围。说得通?我现在正朝着这个方向努力。
    • @srkdb 我更新了您的新案例的答案。仅当行以 datetime 开头时,您才需要检查时间。
    • 其实更新后的代码如果还在时间范围内,是不包含中间的文本行的。
    • @srkdb 请举例说明更新后的代码不起作用。
    猜你喜欢
    • 2019-09-15
    • 2012-11-10
    • 2011-11-26
    • 2018-06-22
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多