Python,计算时差

时间:2017-01-06 04:51:41

标签: python-2.7 strptime

我正在解析从多个来源生成的日志,并以下列格式连接在一起形成一个巨大的日志文件;

userid

.... (这里有数十个SR号码)

现在,我正在寻找一种解析日志的智能方法,以便计算每个testNumber和SR编号的时间差异(以秒为单位) 喜欢 My_testNumber:14它减去SR 284和SR 111时间(这里差值为1秒),对于SR 284和299它是2秒,依此类推。

1 个答案:

答案 0 :(得分:1)

您可以解析发布的日志文件并相应地保存相应的数据。然后,您可以使用数据来获取时间差异。以下应该是一个不错的开始:

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}

编辑:

解析文件而不依赖于日期周围的星号:

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}

我希望这证明有用。

相关问题