简体   繁体   English

使用 python 从日志文件计算增量时间

[英]Calculating delta time from a log file using python

I am stuck on trying to find delta of the first and last time stamp from a log file我一直试图从日志文件中查找第一个和最后一个时间戳的增量

Here is a section of the log file这是日志文件的一部分

[2020-07-31 15:49:22,015][SRC.Env][I]:Reading 
[2020-07-31 15:49:22,015][SRC.Env][I]:Finished Initilization 
[2020-07-31 15:49:22,052][SRC][I]:Creating link
[2020-07-31 15:49:22,053][SRC][I]:Starting
.
.
.
[2020-08-03 09:17:29,351][SRC.Upload][I]:Finished

The following is what I have done thus far以下是我到目前为止所做的

import re
from dateutil import parser

with open('run.log') as run_log:
  times = [re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}',
      line) for line in run_log.readlines() if 'SRC' in line]
print(times)

time_delta = parser.parse(times[-1]) - parser.parse(times[0])
print(time_delta)

When I print times, it seemed to show all the times (as expected) [['2020-07-31 15:49:22,011'], ['2020-07-31 15:49:22,015'],...['2020-08-03 09:17:29,351']]当我打印时间时,它似乎显示所有时间(如预期) [['2020-07-31 15:49:22,011'], ['2020-07-31 15:49:22,015'],...['2020-08-03 09:17:29,351']]

However, when I am trying to substract the first time to the last time, I recieve the following error但是,当我尝试第一次减去最后一次时,我收到以下错误

    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\dateutil\parser\_parser.py", line 646, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\dateutil\parser\_parser.py", line 725, in _parse
    l = _timelex.split(timestr)         # Splits the timestr into tokens
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\dateutil\parser\_parser.py", line 207, in split
    return list(cls(s))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\dateutil\parser\_parser.py", line 76, in __init__
    '{itype}'.format(itype=instream.__class__.__name__))
TypeError: Parser must be a string or character stream, not list

I decided to learn how to code about two months ago, so any assistance would really help in my progress.大约两个月前,我决定学习如何编码,所以任何帮助都会对我的进步有所帮助。 Thanks:)谢谢:)

The problem is re.findall() returns a list .问题是re.findall()返回一个list

You can access the single element using re.findall(pattern, s)[0]您可以使用re.findall(pattern, s)[0]访问单个元素

import re
from dateutil import parser

with open('run.log') as run_log:
    times = [re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}',
                        line)[0] for line in run_log.readlines() if 'SRC' in line]
print(times)

time_delta = parser.parse(times[-1]) - parser.parse(times[0])
print(time_delta)

Output: Output:

['2020-07-31 15:49:22,015', '2020-07-31 15:49:22,015', '2020-07-31 15:49:22,052', '2020-07-31 15:49:22,053', '2020-08-03 09:17:29,351']
2 days, 17:28:07.336000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM