I have a bunch of dates without years in the following format:
Thu Apr 10
Mon Mar 28
Is there a simple way in python to identify the year these dates come from?
I have a bunch of dates without years in the following format:
Thu Apr 10
Mon Mar 28
Is there a simple way in python to identify the year these dates come from?
Of course there will be cases where more than one year is valid, but assuming that you would like to take the later year, if there is such a case, then this algorithm should work fine.
For each string
Take the Month(Apr) and the Date(10)
For each year from this year down to whenever you'd like to stop
Make a datetime object with the Month, Date, and Year
If the datetime object's .weekday is the same as the Day you have (Thu)
You've found the right year
Or in python, it may look something like this:
import datetime
with open("yourFile") as f:
for line in f:
day = #get the day from the line (0-30)
month = #get the number representing the month from the line (0-11)
year = 2016
while True:
testDate = datetime.date(year, month, day)
weekday = #turn the string "Thu" or "Mon" into the number value
#it represents (0-6)
if testDate.weekday == weekday:
#You have found a matching year!!!
else:
year = year + 1
The code below should work, you'll have to pick a year to start from and a direction to go to as step
(-1
for back in time, 1
for forward in time) and it'll give you the first year it encounters for which the condition is true:
import datetime
weekdays = {'Mon': 0, 'Tue': 1, 'Wed': 2, 'Thu': 3, 'Fri': 4, 'Sat': 5, 'Sun': 6}
months = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
dates = ['Thu Apr 10', 'Mon Mar 28']
startYear = 2016
step = -1
years = []
for date in dates:
[weekDay, month, day] = date.split(' ')
day = int(day)
year = startYear
while True:
if datetime.date(year, months[month], day).weekday() == weekdays[weekDay]:
years.append(year)
break;
else:
year = year + step