1

I am parsing a log file and one element contains the date as a String:

Tue Mar 31 20:24:23 BST 2015

The date is in element[i][0] of a 2DList

What I am a little stumped on (without going about this in some awful compare and replace manner) is how to turn this date into something comparable in Python.

I get a few entries in a log file which are within a few minutes of each other, so I would like to group these as one.

Tue Mar 31 20:24:23 BST 2015
Tue Mar 31 20:25:45 BST 2015
Tue Mar 31 20:26:02 BST 2015

What options can be suggested?

I am aware that I can input logic to replace 'Mar' with 3, remove Day Tue/Wed etc strings, but everything else is somewhat needed.

Would it be acceptable to replace a : with / I can then split the date into a list by its ' ' delimiter, then compare the 20/26/02 element, but before I go and do all that, is there a built in way? I have searched and found python datetime 1, which I would use after a lot of replacing values.

Really, I'm looking for a built in method!

1

1 Answer 1

2

You can use the datetime.datetime.strptime.

Here are format specifiers.

Something like datetime.strptime(your_string, "%a %b %d %H:%M:%S %Z %Y") should do the work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.