Lev
Lev

Reputation: 1930

parse datetime string with extra characters

I have a datetime field with a really silly format:

2016423123

where 2016 is the year, 4 is the month, 23 is the day, and 123 is irrelevant.

Does the format parameter of datetime.strptime support directives (like a regex) to ignore characters in the input string?

I know it is possible to just remove the extra characters and then feed the result to the parser, but I have a problem where I have several possible formats (not my invention...), and I was thinking of using try/catch to parse them.

Upvotes: 3

Views: 2216

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

If the last 3 characters of your string are irrelevant, skip it during the conversion of your string to datetime object using string slicing. For example:

>>> my_str[:-3]
'2016423'

Here is the usage with datetime.strptime:

>>> my_str = '2016423123'
>>> from datetime import datetime

#                             v removing last 3 characters from string
>>> datetime.strptime(my_str[:-3], '%Y%m%d')
datetime.datetime(2016, 4, 23, 0, 0)

Other examples, with different combination of single/double digit month/day:

>>> my_str = '20160423123'
>>> datetime.strptime(my_str[:-3], '%Y%m%d')
datetime.datetime(2016, 4, 23, 0, 0)

>>> my_str = '2016043123'
>>> datetime.strptime(my_str[:-3], '%Y%m%d')
datetime.datetime(2016, 4, 3, 0, 0)

>>> my_str = '201643123'
>>> datetime.strptime(my_str[:-3], '%Y%m%d')
datetime.datetime(2016, 4, 3, 0, 0)

>>> my_str = '2016123123'
>>> datetime.strptime(my_str[:-3], '%Y%m%d')
datetime.datetime(2016, 12, 3, 0, 0)

Upvotes: 1

Related Questions