如何比较同样有版本号的日志行中的时间戳



我正在尝试查找具有最新时间戳的版本号日志行,目前我正在尝试使用parse_version来完成此操作。

日志行示例:

2018-05-08T15:25:02.053Z 00000000-0000-0000-0000-000000000000 > LVL:2 RC: version: 2.11.0.10451
2018-05-08T21:27:14.2049217Z> <INFO >: Version: 2.10.0.23960
2018-05-08T21:18:53.0428568Z> <INFO >: Version: 2.12.1.26051

虽然最后一条日志行的版本号是最新的,但我希望存储并显示第二条日志行,即使它的版本号较低,因为它有最新的时间戳。目前,使用parse_version比较,我正在打印最后一行日志(我猜是因为时间戳和版本之间的组合值总体上更大(。

以下是我当前的代码:

for line in f: #For simplicity sake, I won't include my code above this line because it's just for looping through the folder to find the log lines
#0strip out x00 from read content, in case it's encoded differently
line = line.replace('x00', '')
#Regular expressions for finding the log lines in the folder
RE2 = r"^.+INFO.+Version.+"
RE3 = r"^.+RC: version"
latest_version_line = ''
#Find the general matches, and get the version line with the latest time stamp
pattern2 = re.compile('('+RE2+'|'+RE3+')', re.IGNORECASE)
for match2 in pattern2.finditer(line):
if parse_version(line) > parse_version(latest_version_line):
latest_version_line = line
print(latest_version_line)

提前感谢!

由于您希望存储和显示第二个最新版本号,您可以简单地使用另一个变量来保留当前最新版本编号,然后将其替换为新的最新版本。

更改:

if parse_version(line) > parse_version(latest_version_line):
latest_version_line = line
print(latest_version_line)

至:

if parse_version(line) > parse_version(latest_version_line):
second_latest_version_line = latest_version_line
latest_version_line = line
print(latest_version_line)
print(second_latest_version_line)

最新更新