Question

This is a simplified version of what my problem is but here is the gist of it: I have a script that sorts emails by using Regex to find specific patterns. One of the items I sort by is the date an email was received. I use the datetime module to format the date string, like so:

    s1 = datetime.strptime(regex_obtained_str, '%B %d, %Y, %I:%M:%S %p').strftime('%m/%d/%Y %I:%M:%S %p')

Now, let's say I have parsed 4 emails and these are the results from the Regex function:

    s1 = 'March 15, 2013 8:58:34 PM'
    s2 = 'March 15, 2013 11:10:10 AM'
    s3 = 'March 15, 2013 11:16:06 AM'
    s4 = 'April 15, 2013 12:02:04 PM'

Which turns into this after strftime formatting:

    s1 = '03/15/2013 08:58:34 PM'
    s2 = '03/15/2013 11:10:10 AM'
    s3 = '03/15/2013 11:16:06 AM'
    s4 = '04/15/2013 12:02:04 PM'

If I put these all in a list in random order and perform a sort() function, like so:

    L = [s2, s1, s3, s4]
    L.sort()

The output is:

    ['03/15/2013 08:58:34 PM', '03/15/2013 11:10:10 AM', '03/15/2013 11:16:06 AM', '04/15/2013 12:02:04 PM']

As you can see, the "AM" and "PM" is not being taken into account when sorting. I've looked at a NUMBER of other questions similar to this on StackOverflow, and all of them suggest changing %H to %I in the strptime()/stftime() functions to account for the 12 hour clock, but I already have done that. Can anyone please suggest how to take %p into account when sorting? Thanks!

Was it helpful?

Solution

The issue here is that you are sorting strings - Python doesn't know that these strings are times, so it just sorts them as strings (which happens to almost work).

So, the solution is, sort them when they are date objects, at this point, Python has the information it needs to sort them correctly:

dates = sorted([datetime.strptime(regex_obtained_str, '%B %d, %Y, %I:%M:%S %p') 
                for regex_obtained_str in strings])
l = [date.strftime('%m/%d/%Y %I:%M:%S %p') for date in dates]

Here using list comprehensions to apply the formatting easily to all of the objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top