Convert python short month name to fully qualified name

How can I convert the abbreviated date of the month, for example. Apr in python for full name?

+5
source share
5 answers

If you insist on using datetime according to your tags, you can convert the short version of the month to a datetime object, and then reformat it with the full name:

 import datetime datetime.datetime.strptime('apr','%b').strftime('%B') 
+6
source

The following is a method to use the calendar library.

 >>> import calendar >>> calendar.month_name [list(calendar.month_abbr).index('Apr')] 'April' >>> 
+3
source

a simple dictionary will work

eg,

 month_dict = {"jan" : "January", "feb" : "February" .... } 

month_dict ["january"]

'January'

+1
source

One quick and dirty way:

 conversions = {"Apr": "April", "May": "May", "Dec": "December"} date = "Apr" if date in conversions: converted_date = conversions[date] 
+1
source

This is for mixed abbreviations and full month names, for example:

 Dec 31, 2017 December 31, 2017 Aug 31, 2017 Feb 10, 2018 March 1, 2018 Jun 2, 2019 

If you do this in a Pandas Dataframe with actual dates, and you do not want June to complete the display in Junee :

 def replace_month_abrev(date_string): month_dict = {"Jan ": "January ", "Feb ": "February ", "Mar ": "March ", "Apr ": "April ", "May ": "May ", "Jun ": "June ", "Jul ": "July ", "Aug ": "August ", "Sep ": "September ", "Sept ": "September ", "Oct ": "October ", "Nov ": "November ", "Dec ": "December "} # find all dates with abrev abrev_found = filter(lambda abrev_month: abrev_month in date_string, month_dict.keys()) # replace each date with its abbreviation for abrev in abrev_found: date_string = date_string.replace(abrev, month_dict[abrev]) # return the modified string (or original if no states were found) return date_string combined['deadline'].map(replace_month_abrev) 
0
source

Source: https://habr.com/ru/post/1258307/


All Articles