月号到月号,反之亦然。

时间:2020-12-22 18:17:19

I am trying to create a function that can convert a month number to an abbreviated month name or an abbreviated month name to a month number. I thought this might be a common question but I could not find it online.

我正在尝试创建一个函数,该函数可以将一个月号转换为一个缩写的月名,或者将一个缩写的月名转换为一个月号。我想这可能是一个常见的问题,但我在网上找不到。

I was thinking about the calendar module. I see that to convert from month number to abbreviated month name you can just do calendar.month_abbr[num]. I do not see a way to go the other direction though. Would creating a dictionary for converting the other direction be the best way to handle this? Or is there a better way to go from month name to month number and vice versa?

我在考虑日历模块。我看到,要将月号转换为缩写的月名,只需执行calendar.month_abbr[num]。不过,我看不出有什么路可走。创建一个用于转换另一个方向的字典是处理这个问题的最佳方式吗?还是有更好的方法从一个月的名字到一个月的数字,反之亦然?

8 个解决方案

#1


56  

Creating a reverse dictionary would be a reasonable way to do this, because it's pretty simple:

创建一个反向字典是一种合理的方法,因为它非常简单:

import calendar
dict((v,k) for k,v in enumerate(calendar.month_abbr))

or in recent versions of Python (2.7+) which support dictionary comprehension:

或在最近版本的Python(2.7+)中支持字典理解:

{v: k for k,v in enumerate(calendar.month_abbr)}

#2


55  

Just for fun:

只是为了好玩:

from time import strptime

strptime('Feb','%b').tm_mon

#3


29  

Using calendar module:

使用日历模块:

Number-to-Abbr calendar.month_abbr[month_number]

Number-to-Abbr calendar.month_abbr[month_number]

Abbr-to-Number list(calendar.month_abbr).index(month_abbr)

Abbr-to-Number列表(calendar.month_abbr).index(month_abbr)

#4


13  

Here's yet another way to do it.

这里还有另一种方法。

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]

#5


9  

Here is a more comprehensive method that can also accept 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

#6


6  

Information source: Python Docs

信息来源:Python文档

To get month number from month name use datetime module

从月份名使用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')

#7


5  

One more:

一个:

def month_converter(month):
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    return months.index(month) + 1

#8


0  

To get month name using month number, you can use time:

要使用月号获取月名,可以使用time:

import time

mn = 11
print time.strftime('%B', time.struct_time((0, mn, 0,)+(0,)*6)) 

'November'

#1


56  

Creating a reverse dictionary would be a reasonable way to do this, because it's pretty simple:

创建一个反向字典是一种合理的方法,因为它非常简单:

import calendar
dict((v,k) for k,v in enumerate(calendar.month_abbr))

or in recent versions of Python (2.7+) which support dictionary comprehension:

或在最近版本的Python(2.7+)中支持字典理解:

{v: k for k,v in enumerate(calendar.month_abbr)}

#2


55  

Just for fun:

只是为了好玩:

from time import strptime

strptime('Feb','%b').tm_mon

#3


29  

Using calendar module:

使用日历模块:

Number-to-Abbr calendar.month_abbr[month_number]

Number-to-Abbr calendar.month_abbr[month_number]

Abbr-to-Number list(calendar.month_abbr).index(month_abbr)

Abbr-to-Number列表(calendar.month_abbr).index(month_abbr)

#4


13  

Here's yet another way to do it.

这里还有另一种方法。

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]

#5


9  

Here is a more comprehensive method that can also accept 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

#6


6  

Information source: Python Docs

信息来源:Python文档

To get month number from month name use datetime module

从月份名使用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')

#7


5  

One more:

一个:

def month_converter(month):
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    return months.index(month) + 1

#8


0  

To get month name using month number, you can use time:

要使用月号获取月名,可以使用time:

import time

mn = 11
print time.strftime('%B', time.struct_time((0, mn, 0,)+(0,)*6)) 

'November'