我有几个日期字符串,其格式为MM DD(st,nd,rd,th)YYYY HH:MM am。对于我来说,将(st,nd,rd,th)替换为空字符串''是什么最pythonic方式?
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
答案 0 :(得分:3)
最pythonic的方法是使用dateutil
。
from dateutil.parser import parse
import datetime
t = parse("Mar 2nd 2017 00:00 am")
# you can access the month, hour, minute, etc:
t.hour # 0
t.minute # 0
t.month # 3
然后,您可以使用t.strftime()
,其结果字符串的格式为以下任意格式:http://strftime.org/
如果您想要更多适当的时间表示(例如在适当的语言环境中),那么您可以t.strftime("%c")
,或者您可以轻松地将其格式化为您的答案想要上面。
这比正则表达式匹配更安全,因为dateutil
是标准库的一部分,并返回一个简洁的datetime
对象。
答案 1 :(得分:0)
您可以使用正则表达式,如下所示:
import re
strings = ['Mar 1st 2017 00:00 am', 'Mar 2nd 2017 00:00 am', 'Mar 3rd 2017 00:00 am', 'Mar 4th 2017 00:00 am']
for string in strings:
print(re.sub('(.*? \d+)(.*?)( .*)', r'\1\3', string))
这会给你:
Mar 1 2017 00:00 am
Mar 2 2017 00:00 am
Mar 3 2017 00:00 am
Mar 4 2017 00:00 am
如果您想限制它,请st
nd
rd
th
:
print(re.sub('(.*? \d+)(st|nd|rd|th)( .*)', r'\1\3', string))