Last active
January 16, 2023 16:22
-
-
Save miketahani/77f091eee50ee476e81d to your computer and use it in GitHub Desktop.
answer to a NICAR-L question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from datetime import datetime as dt | |
fake_dates = '10-Jan-2011, 01-Feb-02, ##-Bla-##, 01-Bla-2014' | |
# you can probably do some lookahead assertion at the end of the regex to only capture 2 digits or the last 2 digits | |
# instead of the mess below [1] | |
date_finder = re.compile('(\d{2}-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4})') | |
dates = re.findall(date_finder, fake_dates) | |
# two-digit date or four-digit date | |
digits = ['%d-%b-%y', '%d-%b-%Y'] | |
# [1] this is "the mess below" | |
new_dates = [dt.strptime(date, digits[len(date) > 9]).strftime('%Y-%m-%d') for date in dates] | |
print new_dates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment