计算月份数量的月份数量 [英] Figure out month number from month abbrievation

查看:271
本文介绍了计算月份数量的月份数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 -

我正在解析finger命令的输出,并想知道

某事......如果我得到一个月的消除(例如Jan),是什么'

计算月份数的最佳方法?我看到有一个名为month_abbr的东西。在日历模块中。但是,当我尝试执行calendar.month_abbr.index(" Jan")时,我得到_localized_month

实例没有属性''index''。 "所以似乎month_abbr不是
常规列表。我现在这样做:


def month_number(monthabbr):

"""返回monthabbr的月份数;例如"扬" - > 1.""

索引,枚举日期(calendar.month_abbr):

if day == monthabbr:

返回索引


哪个效果不错,但并不是很聪明。我很擅长使用
Python;我在这里错过了什么?

谢谢 - 比尔。

Hello --
I''m parsing the output of the finger command, and was wondering
something...If I''m given a month abbrievation (such as "Jan"), what''s
the best way to figure out the month number? I see that there''s
something called "month_abbr" in the calendar module. However, when I
try to do calendar.month_abbr.index("Jan"), I get "_localized_month
instance has no attribute ''index''." So it seems that month_abbr isn''t
a regular list. I''m currently doing it this way:

def month_number(monthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calendar.month_abbr):
if day == monthabbr:
return index

which works well enough but isn''t very clever. I''m pretty new to
Python; what am I missing here?
Thanks -- Bill.

推荐答案

" Bill"写道:
"Bill" wrote:
我正在解析finger命令的输出,并想知道
某事......如果我得到一个月的消除(例如 ; Jan"),什么是找出月份数的最佳方法?我看到有一个名为month_abbr的东西。在日历模块中。但是,当我尝试执行calendar.month_abbr.index(" Jan")时,我得到_localized_month
实例没有属性''index''。所以似乎month_abbr不是一个常规列表。我现在这样做:

def month_number(monthabbr):
"""返回monthabbr的月份数;例如"扬" - > 1."
索引,枚举日期(calendar.month_abbr):
如果天== monthabbr:
返回索引

哪些有效很好,但不是很聪明。我是Python的新手;我在这里想念的是什么?
I''m parsing the output of the finger command, and was wondering
something...If I''m given a month abbrievation (such as "Jan"), what''s
the best way to figure out the month number? I see that there''s
something called "month_abbr" in the calendar module. However, when I
try to do calendar.month_abbr.index("Jan"), I get "_localized_month
instance has no attribute ''index''." So it seems that month_abbr isn''t
a regular list. I''m currently doing it this way:

def month_number(monthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calendar.month_abbr):
if day == monthabbr:
return index

which works well enough but isn''t very clever. I''m pretty new to
Python; what am I missing here?




a我想是几件事。


....首先,你可以使用list(seq)将任何序列转换为列表对象,

所以你可以做


返回列表(calendar.month_abbr).index(monthabbr)<如果你喜欢在一行中做事,那么


....但它看起来好像是本地化这个词的含义。你不清楚

;如果您更改了区域设置,则会翻译这些名称:



a couple of things, I think.

.... first, you can use list(seq) to convert any sequence to a list object,
so you could do

return list(calendar.month_abbr).index(monthabbr)

if you prefer to do things in one line.
.... but it also looks as if the meaning of the word "localized" isn''t clear to
you; if you changed the locale, those names will be translated:

list(calendar.month_abbr)
list(calendar.month_abbr)



['''',''jan'',''feb'',''mar'',''apr'',''maj' ',''jun'',''jul'',''aug'',''sep'',''okt'',''nov'',''dec'']


会导致你的手指程序中断...


....而且我很确定你可以写下缩写

的时间远远少于你撰写邮件的时间;-)


MONTHS = ['''',

' 'Jan'',''Feb'',''Mar'',''Apr'',''May'',''Jun'',

''Jul'',' 'Aug'',''Sep'',''Oct'',''Nov'',''Dec''

]


month_numb er = MONTHS.index

< / F>


['''', ''jan'', ''feb'', ''mar'', ''apr'', ''maj'', ''jun'', ''jul'', ''aug'', ''sep'', ''okt'', ''nov'', ''dec'']

which will cause your finger program to break...

.... and I''m quite sure that you could have written down the abbreviations
in far less time than it took you to compose that mail ;-)

MONTHS = ['''',
''Jan'', ''Feb'', ''Mar'', ''Apr'', ''May'', ''Jun'',
''Jul'', ''Aug'', ''Sep'', ''Oct'', ''Nov'', ''Dec''
]

month_number = MONTHS.index

</F>


Bill写道:
你好 -
我正在解析finger命令的输出,并且想知道某些事情......如果给我一个月的消除(例如Jan),什么是了解月份数的最佳方法?
我看到有一些名为month_abbr的东西。在日历模块中。但是,当我尝试执行calendar.month_abbr.index(" Jan")时,我得到_localized_month
实例没有属性''index''。所以似乎month_abbr不是一个常规列表。我现在这样做:

def month_number(monthabbr):
"""返回monthabbr的月份数;例如"扬" - > 1."
索引,枚举日期(calendar.month_abbr):
如果天== monthabbr:
返回索引

哪些有效很好,但不是很聪明。我是Python的新手;我在这里想念的是什么?
谢谢 - 比尔。
Hello --
I''m parsing the output of the finger command, and was wondering
something...If I''m given a month abbrievation (such as "Jan"), what''s
the best way to figure out the month number? I see that there''s
something called "month_abbr" in the calendar module. However, when I
try to do calendar.month_abbr.index("Jan"), I get "_localized_month
instance has no attribute ''index''." So it seems that month_abbr isn''t
a regular list. I''m currently doing it this way:

def month_number(monthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calendar.month_abbr):
if day == monthabbr:
return index

which works well enough but isn''t very clever. I''m pretty new to
Python; what am I missing here?
Thanks -- Bill.




好​​了,你可以定义你的功能的等价物


month_number = list(calendar.month_abbr).index


或者


"! 1月2月3月4月5月6月7月8月9月10月11月12月.split()。index



well, you can define the equivalent of your function with

month_number = list(calendar.month_abbr).index

or else

"! Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split().index


2006年4月12日13:20:28 -0700,谣言说 ;比尔"

< fo ********** @ ftml.net>可能写的:
On 12 Apr 2006 13:20:28 -0700, rumours say that "Bill"
<fo**********@ftml.net> might have written:
你好 -
我正在解析finger命令的输出,并且想知道
某事......如果我''给了一个月的消除(例如Jan),什么是找出月份数的最佳方法?
Hello --
I''m parsing the output of the finger command, and was wondering
something...If I''m given a month abbrievation (such as "Jan"), what''s
the best way to figure out the month number?




试试


导入时间

help(time.strftime)


然后这个*可能*适合你:


month_as_string =" Jan"

time.strptime(month_as_string,"%b")。tm_mon


本地化 (正如弗雷德里克所建议的那样)是我上一句话中* * *
的原因。
-

TZOTZIOY,我说英格兰非常好。

亲爱的保罗,

请停止向我们发送垃圾邮件。

哥林多人



Try

import time
help(time.strftime)

and then this *might* work for you:

month_as_string= "Jan"
time.strptime(month_as_string, "%b").tm_mon

"Localization" (as Fredrik also suggested) is the reason for the *might* in
my previous sentence.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians


这篇关于计算月份数量的月份数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆