Search code examples
pythonstring-formatting

Python strptime with variable text


I have a list of dates as strings. It looks like this:

[
  "January 29-30 Meeting - 2013",
  "March 19-20 Meeting - 2013",
  "April/May 30-1 Meeting - 2013",
  "June 18-19 Meeting - 2013",
  "July 30-31 Meeting - 2013",
  "September 17-18 Meeting - 2013",
  "October 29-30 Meeting - 2013",
  "December 17-18 Meeting - 2013"
]

I need to parse these dates to datetime format.

datetime.strptime("January 29-30 Meeting - 2013", "%B %d-[something] - %Y")
datetime.strptime("January 29-30 Meeting - 2013", "%B [something]-%d [something] - %Y")

Is there any way I can tell strptime, in the format specifier, to ignore the text in [something] since it can be variable? Is there a format specifier for variable text?


Solution

  • You can override the _strptime.TimeRE object with an additional directive that lazily matches any sequence of characters:

    from datetime import datetime
    import _strptime
    TimeRE = _strptime.TimeRE()
    TimeRE.update({'x': '.*?'})
    _strptime._TimeRE_cache = TimeRE
    print(datetime.strptime("January 29-30 Meeting - 2013", "%B %d-%x - %Y"))
    

    This outputs:

    2013-01-29 00:00:00