使用标准模块“re”可以从当前日期捕获/解析3天的条目吗?
下面我想要捕获条目的示例文件。
xx: xxxxxx ; xxxx: 2017-09-2T14:13:17 ; xxxxxxxx: xxxxxxxxxxxxxxxxxxxxxxxxxx
答案 0 :(得分:0)
你必须用re模块提取带有小时的字符串然后:
from datetime import datetime
capturedEntries = []
THREE_DAYS = datetime.timedelta(3)
# I suppose you'are reading lines in another way, but as an example...
for entry in entries:
# I'm assuming each entry follows the format
# xx: xxxxxx ; xxxx: 2017-09-2T14:13:17 ; xxxxxxxx: xxxxxxxxxxxxxxxxxxxxxxxxxx
stringDate = re.search(r'\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}:\d{2}', line)
if stringDate.group():
parsedDate = datetime.strptime(stringDate.group(), '%Y-%m-%dT%H:%M:%S')
timeFromDate = (datetime.today() - parsedDate)
if timeFromDate > THREE_DAYS:
# We save only the datetimes you asked for in datetime format
# For string format, replace parsedDate for stringDate
capturedEntries.append(parsedDate)