【问题标题】:Calculating time in log file using python使用python计算日志文件中的时间
【发布时间】:2020-06-14 23:48:25
【问题描述】:

我有这个日志文件(log.txt):

omer| (stmt : 0) | adminT|  Connection id - 0
omer| (stmt : 0) | adminT|  Start Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT|  Statement create or replace table amit (x date);
omer| (stmt : 0)| adminT|  Connection id - 0 - Executing - create or replace table amit (x date);
omer| (stmt : 0) | adminT|  Connection id - 0
omer| (stmt : 0) | adminT|  End Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT|  SQL - create or replace table amit (x date);
omer| (stmt : 0) | adminT|  Success
admin| (stmt : 1) | adminT|  Connection id - 0
admin| (stmt : 1) | adminT|  Start Time - 2018-11-06 16:52:14
admin| (stmt : 1) | adminT|  Statement create or replace table amit (x int, y int);
admin| (stmt : 1)| adminT|  Connection id - 0 - Executing - create or replace table amit (x int, y int);
admin| (stmt : 1) | adminT|  Connection id - 0
admin| (stmt : 1) | adminT|  End Time - 2018-11-06 16:52:15
admin| (stmt : 2) | adminT|  Connection id - 0
admin| (stmt : 2) | adminT|  Start Time - 2018-11-06 16:52:19
admin| (stmt : 2) | adminT|  Statement create table amit (x int, y int);
admin| (stmt : 2) | adminT|  Connection id - 0
admin| (stmt : 2) | adminT|  End Time - 2018-11-06 16:52:22
admin| (stmt : 2) | adminT|  SQL - Can't create table 'public.amit' - a table with the same name already exists
admin| (stmt : 2) | adminT|  Failed

我想计算最慢的成功语句,例如:

start time - 2018-11-06 16:52:01 End Time - 2018-11-06 16:52:01 结果是零时间,最后的语句是Success

即使最后一条语句花费了 3 秒,也没有关系,因为该语句最终失败了: Start Time - 2018-11-06 16:52:19 End Time - 2018-11-06 16:52:22Failed 所以没关系。 所以中间语句用了 1 秒并且成功了,所以这是最慢的语句。

我希望使用 python 来实现这段代码,它会产生结果。

【问题讨论】:

    标签: python date date-manipulation


    【解决方案1】:

    您可以将日期保存在日期变量中(您必须导入“日期时间”)

    例如如果你想保存 19.05.2020 9:00 那么你需要编码:

    date = datetime.datetime(year=YOUR YEAR, month=YOUR Month, day=YOUR DAY, hour=YOUR HOUR {And so on})
    

    使用python函数min(),您可以传入多个值,该函数将返回最小的值。 例如:

    print(min(date - date1, date2 - date3, date4 - date5))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2019-07-15
      • 2021-11-19
      相关资源
      最近更新 更多