如何在python中按时间顺序排列事件和数据

时间:2017-04-12 14:26:37

标签: python sorting

我的任务是按时间顺序对以下数据进行排序,并根据时间戳列出数据。

如果有人可以指导我完成逻辑,我可以尝试自己编写代码。

输入:

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : linkManager/link-event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X reason:Port Down]

Timestamp         : 2017-02-13 12:07:26.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : linkManager/link-event
DataFields        : [srcSwName:X, srcPortName:ethernet29, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X reason:Port Down]

Timestamp         : 2017-02-13 12:07:26.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

预期输出:

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : linkManager/link-event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X reason:Port Down]

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : linkManager/link-event
DataFields        : [srcSwName:X, srcPortName:ethernet29, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X reason:Port Down]

Timestamp         : 2017-02-13 12:07:25.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

Timestamp         : 2017-02-13 12:07:26.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

Timestamp         : 2017-02-13 12:07:26.040000 UTC
Event Name        : eventManager/event
DataFields        : [srcSwName:X, srcPortName:ethernet30, dstSwName:X, dstPortName:ethernet3, fabricLinkEventType:X, reason:Port Down]

2 个答案:

答案 0 :(得分:0)

让我们想象这是一个文件

import itertools

with open('filename') as f:
    # Extract blocks by splitting on empty lines
    blocks = [list(val) for key, val in 
              itertools.groupby(f, lambda x: bool(x.rstrip()))
              if key]

    # Now the top line on each block has a timestamp so compare it:
    blocks.sort(key=lambda x: x[0].split(':')[-1])

with open('out_file', 'w') as f:
    # Write back the blocks with a new line separator to new file:
    for block in blocks:
       f.writelines(block + ['\n'])

答案 1 :(得分:0)

您需要read您的文件并将其加载到例如list。然后将TimeStamp表示为UTC时区的第一个值字段转换为timestamp

例如:

Timestamp         : 2017-02-13 12:07:25.040000 UTC
包含2017-02-13 12:07:25.040000

可以使用timestamp模块替换为datetime,如下例所示:

from datetime import datetime
a = '2017-02-13 12:07:25.040000'
b = datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S.%f").timestamp()
print(b)
>>> 1486984045.04

现在,您可以创建method,也可以像这样使用lambda函数:

# Note: x should be a string
get_timestamp = lambda x: datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp()

然后,您可以通过新sortlist函数返回的timestamp数据method lmabda。{/ p>