Back to main.

Calmcode Shorts

datefinder.py logodatefinder.py

Let's say you've got a list of strings that represent dates in an inconsistent format.

date_strings = [
    "March 12 2010",
    "2010-03-12",
    "03/12/2010 12:42:12"
]

You'd like to parse the dates out but you can't use the standard datetime formatter. Before doing it by hand, you might want to try out datefinder first.

import datefinder

[list(datefinder.find_dates(d)) for d in date_strings]
# [[datetime.datetime(2010, 3, 12, 0, 0)],
#  [datetime.datetime(2010, 3, 12, 0, 0)],
#  [datetime.datetime(2010, 3, 12, 12, 42, 12)]]

The datefinder.find_dates function works great a lot of the time! But we need to remember that it isn't perfect. It still needs to "guess" what the correct format is.


Back to main.