您可能希望考虑使用 parsedatetime进行模糊日期匹配。
如果您的日期与 NN(ordinal) Month Year 的格式相当不变,则此方法有效:
dates="""\
16th February 2011
4th April 2009
31st December 2007"""
import re
import time
for date in dates.splitlines():
p=re.findall(r'(\d+)\w\w (\w+) (\d\d\d\d)',date)
d=time.strptime(' '.join(p[0]),'%d %B %Y')
iso=time.strftime('%Y%m%d',d)
print "{0:>20} =>{1:>30} =>{2:>15}".format(date,p,iso)
印刷:
16th February 2011 => [('16', 'February', '2011')] => 20110216
4th April 2009 => [('4', 'April', '2009')] => 20090404
31st December 2007 => [('31', 'December', '2007')] => 20071231