Here is a more complete method that can also take full month names
def month_string_to_number(string): m = { 'jan': 1, 'feb': 2, 'mar': 3, 'apr':4, 'may':5, 'jun':6, 'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12 } s = string.strip()[:3].lower() try: out = m[s] return out except: raise ValueError('Not a month')
Example:
>>> month_string_to_number("October") 10 >>> month_string_to_number("oct") 10
harryh Nov 16 '15 at 13:05 2015-11-16 13:05
source share