Reputation: 20267
I'm parsing a date using Python's datetime.strptime
. The dates are in %Y/%m/%d
format, but I want to be agnostic about the separator used for the dates - /
, -
, .
and any others are all fine.
Right now, I'm using a try
block, but it's a bit verbose.
Is there a simple way to accept any given separator for strptime
?
Upvotes: 5
Views: 7721
Reputation: 103744
You can use named groups and a named back-reference:
import re, datetime
txt='''\
1994-8-22
1970 1 1
2014/01/03
2000.1.1
1999 8 27
1991/02-01 bad date'''
for test in txt.splitlines():
dt=re.match(r'\s*(?P<Y>\d\d\d\d)(?P<sep>\D)(?P<m>\d\d?)(?P=sep)(?P<d>\d\d?)', test)
if dt:
print '"{:10}"=>{:20}=>{}'.format(test,
dt.group(*'Ymd'),
datetime.date(*map(int, dt.group(*'Ymd'))))
That allows you to make sure that the separators are flexible (any non digit) but consistent. ie, this is a good date: 1999/1/2
but this 1999-1/2
maybe is not.
Prints:
"1994-8-22 "=>('1994', '8', '22') =>1994-08-22
"1970 1 1 "=>('1970', '1', '1') =>1970-01-01
"2014/01/03"=>('2014', '01', '03')=>2014-01-03
"2000.1.1 "=>('2000', '1', '1') =>2000-01-01
"1999 8 27 "=>('1999', '8', '27') =>1999-08-27
Upvotes: 1
Reputation: 142116
I'd be tempted to not use strptime, and do something like:
import re
from datetime import datetime
dt = datetime(*map(int, re.findall('\d+', your_string)))
Although your forgo some validation there - such as if there's more than three fields, they'll become arguments to datetime
instead of raising an exception that there's unparsed string.
Upvotes: 0
Reputation: 1121416
You'd have to use a regular expression to normalize the separators:
import re
datetimestring = re.sub('[-.:]', '/', datetimestring)
would replace any -
, .
or :
character with a slash.
Alternatively, use the dateutil.parser.parse()
function to handle arbitrary date-time formats; it is extremely flexible towards separators.
Upvotes: 6