2011-08-04 89 views
8

我有一大堆日期字符串的形式如下: -轉換日期字符串到年月日

30th November 2009 
31st March 2010 
30th September 2010 

我想他們是這樣的: -

YYYYMMDD 

目前我在做這個: -

parsed_date = "30th November 2009" 
    part = parsed_date.split(' ') 
    daymonth = part[0].strip(string.ascii_letters) 
    mytime = daymonth+" "+part[1]+" "+part[2] 
    time_format = "%d %B %Y" 
    cdate = time.strptime(mytime, time_format) 
    newdate = str(cdate[0])+str(cdate[1])+str(cdate[2]) 

它的工作原理,但我敢肯定有一個更好的辦法...

回答

10

嘗試dateutil

from dateutil import parser 

dates = ['30th November 2009', '31st March 2010', '30th September 2010'] 

for date in dates: 
    print parser.parse(date).strftime('%Y%m%d') 

輸出:

20091130 
20100331 
20100930 

,或者如果你想使用標準datetime模塊它做的事:

from datetime import datetime 

dates = ['30th November 2009', '31st March 2010', '30th September 2010'] 

for date in dates: 
    part = date.split() 
    print datetime.strptime('%s %s %s' % (part[0][:-2]), part[1], part[2]), '%d %B %Y').strftime('%Y%m%d') 
+0

謝謝,這就是完美:) –

5

你幾乎可以用strptime and strptime from the datetime module組合做到這一點。

我們遇到的問題是內置格式支持像30 November 2010這樣的日期,但不支持30th November 2010。所以在下面的例子中,我使用a regular expression substitution去除問題字符。 (正則表達式使用後退來查看「st」,「nd」,「rd」或「th」是否前面有一個數字,如果是,則用空字符串替換,從而將其從字符串中移除。 )

>>> import re 
>>> from datetime import datetime 
>>> mydate = "30th November 2009" 
>>> mydate = re.sub("(?<=\d)(st|nd|rd|th)","",mydate) 
>>> mydate 
'30 November 2009' 
>>> mydatetime = datetime.strptime(mydate,"%d %B %Y") 
>>> mydatetime 
datetime.datetime(2009, 11, 30, 0, 0) 
>>> mydatetime.strftime("%Y%M%d") 
'20090030' 
相關問題