首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python,计算时间差

Python,计算时间差
EN

Stack Overflow用户
提问于 2017-01-06 04:51:40
回答 1查看 140关注 0票数 0

我正在解析从多个源生成的日志,并将它们结合在一起,形成一个巨大的日志文件,格式如下;

代码语言:javascript
运行
复制
My_testNumber: 14, JobType = testx.

ABC 2234

**SR 111**
1483529571  1   1   Wed Jan  4 11:32:51 2017    0   4
    datatype someRandomValue
SourceCode.Cpp 588

DBConnection failed

TB 132


**SR 284**
1483529572  0   1   Wed Jan  4 11:32:52 2017    5010400     4
    datatype someRandomXX
SourceCode2.cpp 455

DBConnection Success

TB 102

**SR 299**

1483529572  0   1   **Wed Jan  4 11:32:54 2017**    5010400     4
    datatype someRandomXX
SourceCode3.cpp 455

ConnectionManager Success

……(这里有几十个SR数字)

现在我正在寻找一种解析日志的聪明方法,这样它可以计算每个testNumber和SR数的时间差,比如My_testNumber:14,它减去SR 284和SR 111时间(这里的差是1秒),对于SR 284和299,是2秒等等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-06 06:16:08

您可以解析已发布的日志文件并相应地保存相应的数据。然后,您可以使用数据获取时间差。以下是一个良好的开端:

代码语言:javascript
运行
复制
from itertools import combinations
from itertools import permutations # if order matters
from collections import OrderedDict
from datetime import datetime
import re


sr_numbers = []
dates = []

# Loop through the file and get the test number and times
# Save the data in a list

pattern = re.compile(r"(.*)\*{2}(.*)\*{2}(.*)")
for line in open('/Path/to/log/file'):
    if '**' in line:
        # Get the data between the asterisks
        if 'SR' in line:
            sr_numbers.append(re.sub(pattern,"\\2", line.strip()))
        else:
            dates.append(datetime.strptime(re.sub(pattern,"\\2", line.strip()), '%a %b  %d %H:%M:%S %Y'))
    else:
        continue

# Use hashmap container (ordered dictionary) to make it easy to get the time differences
# Using OrderedDict here to maintain the order of the order of the test number along the file
log_dict = OrderedDict((k,v) for k,v in zip(sr_numbers, dates))

# Use combinations to get the possible combinations (or permutations if order matters) of time differences
time_differences = {"{} - {}".format(*x):(log_dict[x[1]] - log_dict[x[0]]).seconds for x in combinations(log_dict, 2)}

print(time_differences)

# {'SR 284 - SR 299': 2, 'SR 111 - SR 284': 1, 'SR 111 - SR 299': 3}

编辑:

解析文件时不依赖日期附近的星号:

代码语言:javascript
运行
复制
from itertools import combinations
from itertools import permutations # if order matters
from collections import OrderedDict
from datetime import datetime
import re


sr_numbers = []
dates = []

# Loop through the file and get the test number and times
# Save the data in a list

pattern = re.compile(r"(.*)\*{2}(.*)\*{2}(.*)")
for line in open('/Path/to/log/file'):
    if 'SR' in line:
        current_sr_number = re.sub(pattern,"\\2", line.strip())
        sr_numbers.append(current_sr_number)
    elif line.strip().count(":") > 1:
        try:
            dates.append(datetime.strptime(re.split("\s{3,}",line)[2].strip("*"), '%a %b  %d %H:%M:%S %Y'))
        except IndexError:
            #print(re.split("\s{3,}",line))
            dates.append(datetime.strptime(re.split("\t+",line)[2].strip("*"), '%a %b  %d %H:%M:%S %Y'))
    else:
        continue


# Use hashmap container (ordered dictionary) to make it easy to get the time differences
# Using OrderedDict here to maintain the order of the order of the test number along the file
log_dict = OrderedDict((k,v) for k,v in zip(sr_numbers, dates))

# Use combinations to get the possible combinations (or permutations if order matters) of time differences
time_differences = {"{} - {}".format(*x):(log_dict[x[1]] - log_dict[x[0]]).seconds for x in combinations(log_dict, 2)}

print(time_differences)

# {'SR 284 - SR 299': 2, 'SR 111 - SR 284': 1, 'SR 111 - SR 299': 3}

我希望这证明是有用的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41499134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档