The best way to filter a log by a dates range in python

前端 未结 5 1078
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 19:51

What\'s the best way to print log lines that match a datetime range. For example:

I would like to print only lines with dates from: 2012/09/30-00:00:10 to: 2012/09/

5条回答
  •  生来不讨喜
    2021-01-13 20:07

    Actullay, the log format allows to compare date strings without their conversion to datetime.

    with open('mylog.log','r') as f:
        for line in f:
            d = line.split(" ",1)[0] 
            if d >= '2012/09/30-00:00:10' and d <= '2012/09/30-00:00:13':
                print line
    

提交回复
热议问题