我有以下格式更改日期,正則表達式和python一個布赫
16th February 2011
4th April 2009
31st December 2007
一串日期,我想將其更改爲這種格式
20110216
20090404
20071231
我想這樣做在python中,我試過regex
和time
,但無法讓我的頭轉過來。
我有以下格式更改日期,正則表達式和python一個布赫
16th February 2011
4th April 2009
31st December 2007
一串日期,我想將其更改爲這種格式
20110216
20090404
20071231
我想這樣做在python中,我試過regex
和time
,但無法讓我的頭轉過來。
你不妨考慮parsedatetime zy日期匹配。
如果您的日期是從NN(序)月是一年的格式相當不變,這個工程:
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
沒有進口,用於學習目的。
個月是一個月的字典。
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
做的兩個步驟:
使用正則表達式(\d+)([a-z]{2})\s+([A-Za-z]+)\s+(\d{4})
與空字符串
使用time.strptime(string[, format])
更換第二組轉換日期格式,你需要
您可以使用正則表達式來獲取信息,然後把它變成日期。
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")
通常情況下,我推薦使用'time.strptime',但這些序號會導致問題。 –