我正在尝试创建一个函数,可以将月份数字转换为缩写的月份名称或缩写的月份名称转换为月份数字.我认为这可能是一个常见的问题,但我无法在网上找到它.
我在考虑日历模块.我看到你可以将月份数转换为缩写月份名称calendar.month_abbr[num]
.我没有办法走向另一个方向.创建用于转换其他方向的字典是否是处理此问题的最佳方法?或者有更好的方法从月份名称到月份号码,反之亦然?
Dav*_*d Z 73
创建反向字典是一种合理的方法,因为它非常简单:
import calendar
dict((v,k) for k,v in enumerate(calendar.month_abbr))
Run Code Online (Sandbox Code Playgroud)
或者在最新版本的Python(2.7+)中支持字典理解:
{v: k for k,v in enumerate(calendar.month_abbr)}
Run Code Online (Sandbox Code Playgroud)
小智 67
纯娱乐:
from time import strptime
strptime('Feb','%b').tm_mon
Run Code Online (Sandbox Code Playgroud)
dio*_*ino 43
使用日历模块:
号到缩写
calendar.month_abbr[month_number]
缩写到数
list(calendar.month_abbr).index(month_abbr)
Gi0*_*i0s 16
这是另一种方法.
monthToNum(shortMonth):
return{
'Jan' : 1,
'Feb' : 2,
'Mar' : 3,
'Apr' : 4,
'May' : 5,
'Jun' : 6,
'Jul' : 7,
'Aug' : 8,
'Sep' : 9,
'Oct' : 10,
'Nov' : 11,
'Dec' : 12
}[shortMonth]
Run Code Online (Sandbox Code Playgroud)
har*_*ryh 13
这是一个更全面的方法,也可以接受完整的月份名称
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')
Run Code Online (Sandbox Code Playgroud)
例:
>>> month_string_to_number("October")
10
>>> month_string_to_number("oct")
10
Run Code Online (Sandbox Code Playgroud)
the*_*der 11
信息来源:Python Docs
要从月份名称获取月份号,请使用datetime模块
import datetime
month_number = datetime.datetime.strptime(month_name, '%b').month
# To get month name
In [2]: datetime.datetime.strftime(datetime.datetime.now(), '%a %b %d, %Y')
Out [2]: 'Thu Aug 10, 2017'
# To get just the month name, %b gives abbrevated form, %B gives full month name
# %b => Jan
# %B => January
dateteime.datetime.strftime(datetime_object, '%b')
Run Code Online (Sandbox Code Playgroud)
Ely*_*755 10
完整月份名称到月份编号(例如January、February等):
import datetime
month_name = 'January'
month_num = datetime.datetime.strptime(month_name, '%B').month
print(month_num, type(month_num))
>> 1 <class 'int'>
Run Code Online (Sandbox Code Playgroud)
部分月份名称到月份编号(例如Jan、Feb等):
import datetime
month_name = 'Feb'
month_num = datetime.datetime.strptime(month_name, '%b').month
print(month_num, type(month_num))
>> 2 <class 'int'>
Run Code Online (Sandbox Code Playgroud)
您还可以将其格式化为两位数表示形式:
month_num = 3
formatted = f"{month_num:02}"
print(formatted, type(formatted))
>> 03 <class 'str'>
Run Code Online (Sandbox Code Playgroud)
月份编号到完整的月份名称(无论是否为两位数,字符串或整数)(例如'01'、1等):
import datetime
month_num = '04' # month_num = 4 will work too
month_name = datetime.datetime(1, int(month_num), 1).strftime("%B")
print(month_name)
>> April
Run Code Online (Sandbox Code Playgroud)
月份编号到部分月份名称(无论是否为两位数,字符串或整数)(例如'01'、1等):
import datetime
month_num = 5 # month_num = '05' will work too
month_name = datetime.datetime(1, int(month_num), 1).strftime("%b")
print(month_name)
>> May
Run Code Online (Sandbox Code Playgroud)
小智 8
多一个:
def month_converter(month):
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
return months.index(month) + 1
Run Code Online (Sandbox Code Playgroud)