-1

I have a bunch of dates in the following format

16th February 2011
4th April 2009
31st December 2007

And i want to change them to this format

20110216
20090404
20071231

I want to do this in python, I've tried regex and time, but can't get my head round it.

4

4 回答 4

4

您可能希望考虑使用 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
于 2012-10-17T21:13:48.093 回答
0

分两步进行:

  1. 使用正则表达式(\d+)([a-z]{2})\s+([A-Za-z]+)\s+(\d{4})用空字符串替换第二组

  2. 用于time.strptime(string[, format])将日期转换为您需要的格式

于 2012-10-17T21:12:35.687 回答
0

您可以使用正则表达式获取信息,然后使用 strptime 将其转换为日期。

import datetime
import re
date_re = re.compile("^([0-9]+)[a-z]* (.+)$")
example = "16th February 2011"
m = date_re.match(example)
dt = datetime.datetime.strptime("%s %s" % (m.group(1), m.group(2)), "%d %B %Y")
print dt.strftime("%Y%m%d")
于 2012-10-17T21:15:58.843 回答
0

没有进口,用于学习目的。

月份是月份的字典。

months = {"January":"01","February":"02",...}    
# make sure all entries are strings, not integers

for entry in entries:
    # split by spaces.  this is multiple assignment.  
    # the first split gets assigned to date, the second, to month, the third, to year.
    day, month, year = entry.split() 

    # parse the date.  the th/rd/nd part is always 2 characters.  
    date = day[:-2]

    if len(date) == 1:
        # make sure the date is two characters long
        date = "0" + date 

    # concatenate
    print year + months[month] + date 
于 2012-10-17T21:10:05.227 回答