Skip to content Skip to sidebar Skip to footer

Can I Use Regexes Within Datetime.strptime Formats?

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those. Like: from __future__ import print_function from da

Solution 1:

No, you can't, only fixed text (so literals) and date-time components are supported.

Just extract the datetime portion first; you can use a regex for that task of course. Not that that is needed in your example, because the datetime portion is a fixed-width chunk of text at the end:

datetime.strptime(input_with_ts[-13:], '%Y%m%d_%H%M')

Post a Comment for "Can I Use Regexes Within Datetime.strptime Formats?"