拉过去3个月 [英] Pull Last 3 Months

查看:81
本文介绍了拉过去3个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个模块可以提取最近3个月的str值?

类似于:

打印lastMonths(3)

['''Sep'',''Aug'',''Jul'']


谢谢

Is there a module that can pull str values for say the last 3 months?
Something like:
print lastMonths(3)

[''Sep'', ''Aug'', ''Jul'']

Thanks

推荐答案

是否有一个模块可以提取最近3个月的str值?
Is there a module that can pull str values for say the last 3 months?

类似于:


打印lastMonths(3)


[' 'Sep'',''Aug'',''Jul'']
Something like:

print lastMonths(3)

[''Sep'', ''Aug'', ''Jul'']



我认为没有内置的东西。它有点令人沮丧,因为timedelta不会接受月这样的事情。参数

什么时候会有所帮助(但是多少天是在b / b
月 - delta会逐月变化的。)


它有些不雅,但这对我有用:


导入日期时间


def last_months(几个月):

断言月0

d = datetime.date.today()

m = d.strftime(''%b'')

收益m

而第1个月:

d - = datetime.timedelta(天数= 28)

m2 = d.strftime(''%b'')

如果m2< m:

m = m2

个月 - = 1
收益率m


打印列表(last_months(3))
$ lastbs(24)中的b $ b:打印月份


替代方案可能是从当月减去一个

,如果它降到1以下,则减少

年和将月份重置为12.同样模糊:


def lastN(月):

断言月份0

d = datetime.date.today()
$ x $ b for _ in xrange(月):

收益率d.strftime(''%b '')

y,m = d.year,d.month

如果m 1:

m - = 1

else:

m = 12

y - = 1

d = datetime.date(y,m,1)


使用您喜欢的任何一种。


-tkc

I don''t think there''s anything inbuilt. It''s slightly
frustrating that timedelta doesn''t accept a "months" parameter
when it would be rather helpful (but how many days are in a
month-delta is something that changes from month-to-month).

It''s somewhat inelegant, but this works for me:

import datetime

def last_months(months):
assert months 0
d = datetime.date.today()
m = d.strftime(''%b'')
yield m
while months 1:
d -= datetime.timedelta(days=28)
m2 = d.strftime(''%b'')
if m2 <m:
m = m2
months -= 1
yield m

print list(last_months(3))
for month in last_months(24): print month

The alternative would likely be to do something like subtract one
from the current month, and if it drops below 1, decrement the
year and reset the month to 12. Equally fuzzy:

def lastN(months):
assert months 0
d = datetime.date.today()
for _ in xrange(months):
yield d.strftime(''%b'')
y,m = d.year, d.month
if m 1:
m -= 1
else:
m = 12
y -= 1
d = datetime.date(y,m,1)

Use whichever you prefer.

-tkc


10月17日,9:下午59点,Harlin Seritt< harlinser ... @ yahoo.comwrote:
On Oct 17, 9:59 pm, Harlin Seritt <harlinser...@yahoo.comwrote:

是否有一个模块可以提取过去3个月的str值?

类似于:


打印lastMonths(3)


[''Sep'',''Aug'' ,''Jul'']
Is there a module that can pull str values for say the last 3 months?
Something like:

print lastMonths(3)

[''Sep'', ''Aug'', ''Jul'']



你应该看看''datetime''模块。


你可以获得当前月份:

datetime.datetime.now()。month


以及所有缩写月份名称的列表:

[datetime.datetime(1900,i + 1,1).strftime (''%b'')i在范围(12)中

You should take a look at the ''datetime'' module.

You can get the current month:
datetime.datetime.now().month

And a list of all abbreviated month names:
[datetime.datetime(1900, i + 1, 1).strftime(''%b'') for i in range(12)]


>从那里,构建列表不应该太棘手月
>From there, it shouldn''t be too tricky to construct the list of months



你想要的。


-

Paul Hankin

you want.

--
Paul Hankin


更简单的方法,imho:


导入日期时间

m = {

1:''Jan'',2:''Feb'',3:''Mar'',4:''Apr'',5:''May'',6:''Jun'',7 :''Jul'',8:''Aug'',9:''Sep'',10:''Oct'',11:''Nov'',12:''Dec''

}

month = datetime.date.today()。month

if month == 1:

ans = [m [11],m [12],m [1]]

elif月== 2:

ans = [m [11],m [12],m [1]]

其他:

ans = [m [month-2],m [month-1],m [month]]

print ans

Tim Chase写道:
A simpler way, imho:

import datetime
m = {
1:''Jan'',2:''Feb'',3:''Mar'',4:''Apr'',5:''May'',6:''Jun'',7: ''Jul'',8:''Aug'',9:''Sep'',10:''Oct'',11:''Nov'',12:''Dec''
}
month = datetime.date.today().month
if month == 1:
ans = [m[11], m[12], m[1]]
elif month == 2:
ans = [m[11], m[12], m[1]]
else:
ans = [m[month-2], m[month-1], m[month]]
print ans
Tim Chase wrote:

>有没有可以在最近3个月内提取str值的模块吗?
类似于:

打印lastMonths(3)

[''Sep'','' 8月'',''Jul'']
>Is there a module that can pull str values for say the last 3 months?
Something like:

print lastMonths(3)

[''Sep'', ''Aug'', ''Jul'']



我认为没有内置任何东西。它有点令人沮丧,因为timedelta不会接受月这样的事情。参数

什么时候会有所帮助(但是多少天是在b / b
月 - delta会逐月变化的。)


它有些不雅,但这对我有用:


导入日期时间


def last_months(几个月):

断言月0

d = datetime.date.today()

m = d.strftime(''%b'')

收益m

而第1个月:

d - = datetime.timedelta(天数= 28)

m2 = d.strftime(''%b'')

如果m2< m:

m = m2

个月 - = 1
收益率m


打印列表(last_months(3))
$ lastbs(24)中的b $ b:打印月份


替代方案可能是从当月减去一个

,如果它降到1以下,则减少

年和将月份重置为12同样模糊:


def lastN(月):

断言月0

d = datetime.date.today()

for _ in xrange(月):

收益率d.strftime(''%b'')

y,m = d.year, d.month

if m 1:

m - = 1

else:

m = 12

y - = 1

d = datetime.date(y,m,1)


使用您喜欢的任何一种。

-tkc


I don''t think there''s anything inbuilt. It''s slightly
frustrating that timedelta doesn''t accept a "months" parameter
when it would be rather helpful (but how many days are in a
month-delta is something that changes from month-to-month).

It''s somewhat inelegant, but this works for me:

import datetime

def last_months(months):
assert months 0
d = datetime.date.today()
m = d.strftime(''%b'')
yield m
while months 1:
d -= datetime.timedelta(days=28)
m2 = d.strftime(''%b'')
if m2 <m:
m = m2
months -= 1
yield m

print list(last_months(3))
for month in last_months(24): print month

The alternative would likely be to do something like subtract one
from the current month, and if it drops below 1, decrement the
year and reset the month to 12. Equally fuzzy:

def lastN(months):
assert months 0
d = datetime.date.today()
for _ in xrange(months):
yield d.strftime(''%b'')
y,m = d.year, d.month
if m 1:
m -= 1
else:
m = 12
y -= 1
d = datetime.date(y,m,1)

Use whichever you prefer.

-tkc



-

Shane Geiger

IT总监< br $> b $ b全国经济教育委员会
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net


领导经济和金融扫盲运动

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy


这篇关于拉过去3个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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