如何转换缩写的月份,例如在python中Apr
为全名?
答案 0 :(得分:6)
如果您坚持按照标签使用datetime
,则可以将月份的短版本转换为日期时间对象,然后使用全名重新格式化:
import datetime
datetime.datetime.strptime('apr','%b').strftime('%B')
答案 1 :(得分:3)
以下是使用日历库的方法。
>>> import calendar
>>> calendar.month_name [list(calendar.month_abbr).index('Apr')]
'April'
>>>
答案 2 :(得分:1)
一个简单的字典就可以了
例如
month_dict = {"jan" : "January", "feb" : "February" .... }
month_dict [ “一月”]
'一月'
答案 3 :(得分:1)
一种快速而又肮脏的方式:
conversions = {"Apr": "April", "May": "May", "Dec": "December"}
date = "Apr"
if date in conversions:
converted_date = conversions[date]
答案 4 :(得分:1)
SELECT DISTINCT ?s1 ?s2 {
?s1 ?p ?o .
?s2 ?p ?o .
FILTER NOT EXISTS {
?s1 ?p ?o .
?s2 ?p ?o .
?s1 ?p1 ?o1 .
FILTER NOT EXISTS { ?s2 ?p1 ?o1 } }
FILTER NOT EXISTS {
?s1 ?p ?o .
?s2 ?p ?o .
?s2 ?p2 ?o2 .
FILTER NOT EXISTS { ?s1 ?p2 ?o2 } }
FILTER (STR(?s1) < STR(?s2))
}
确保根据需要转换为适当的情况。
类似地,calendar库也具有import calendar
months_dict = dict(zip(calendar.month_abbr[1:], calendar.month_name[1:]))
# {'Jan': 'January', 'Feb': 'February', 'Mar': 'March', 'Apr': 'April', 'May': 'May', 'Jun': 'June', 'Jul': 'July', 'Aug': 'August', 'Sep': 'September', 'Oct': 'October', 'Nov': 'November', 'Dec': 'December'}
和calendar.day_abbr
答案 5 :(得分:0)
这适用于混合缩写和完整月份名称,例如:
Dec 31, 2017
December 31, 2017
Aug 31, 2017
Feb 10, 2018
March 1, 2018
Jun 2, 2019
如果您在Pandas Dataframe中使用实际日期进行操作,那么您就不会这样做
希望June
最终映射到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)