I have a task to sort out the below data in chronological order and list the data based on the timestamp.
If someone can guide me through the logic, I can try to write the code by myself.
Input:
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]
Expected Output:
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]
lets imagine this is in a file
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'])
You need to read
your file and load it into, for example, a list
. Then convert the first value field of TimeStamp
which is represented into UTC
timezone into timestamp
.
For example:
Timestamp : 2017-02-13 12:07:25.040000 UTC
which contains 2017-02-13 12:07:25.040000
could be replaced by timestamp
using datetime
module like this example:
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
Now, you can create a method
or you can simply use a lambda
function like this way:
# Note: x should be a string
get_timestamp = lambda x: datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp()
Then, you can sort
your list
by the returned timestamp
data of your new method
or lmabda
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.