Search code examples
pythonmonthcalendar

Extract month name from raw string?


Extract month name from raw string

'January 2045 Robots'
'2065 March Mars Colony'
'2089 December Alien'

I want to extract month name from raw string, I took an approch to extract it by creating master tuple

s = 'January 2045 Robots'
months_master = ('january','feb','march','april','may','june','july','august','september','october','november','december')
month = [i for i in months_master if i in s.casefold()]
print(month[0])
'january'

Is there any elegent or any pythonic way to achieve this

Note: For now requirement input string only contains single month ( not multiple like s = 'May to December Bio' )


Solution

  • You could import the month names from the built-in calendar module and also use a generator instead of a list comprehension for better efficiency:

    >>> from calendar import month_name
    >>> s = 'January 2045 Robots'
    >>> months = {m.lower() for m in month_name[1:]}  # create a set of month names
    >>> next((word for word in s.split() if word.lower() in months), None)
    'January'
    

    Alternatively, you could use a regular expression:

    >>> from calendar import month_name
    >>> import re
    >>> pattern = '|'.join(month_name[1:])
    >>> re.search(pattern, s, re.IGNORECASE).group(0)
    'January'