我有这样一根线:
old_ActNacd_2016-12-21_07-09-08.txt:100:2016-12-21 07:08:20 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-21_08-21-04.txt:52:2016-12-21 07:21:42 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-21_08-37-50.txt:49:2016-12-21 08:23:34 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-21_15-00-47.txt:49:2016-12-21 08:39:16 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
我试着这样做:
#creating list after taking the string out
log_list = ostring.split('Handle Identity Request. Send Identity Response. timeout: 1550s')
for itr in log_list:
#getting the dates from the log_list
match = re.search(r'\d{4}-\d{2}-\d{2}', itr)
if match:
date = datetime.strptime(match.group(), '%Y-%m-%d').date()
这个过程工作得很好,但我只想在一个操作中完成,而不是在两个步骤中完成(拆分和匹配)
Note:-I want to create a list of dates from the string present between ":" and "space" in the string. I don't want the date present with "_ActNacd_" string.
因此,我将创建一个包含日期的列表:
['2016-12-21','2016-12-21', '2016-12-21', '2016-12-21']
使用^{} ,您可以实现如下所示:
如果每行中只需要第二个日期,请尝试:
输出:
首先尝试按\n拆分,然后可以按行迭代并使用反向子字符串获取日期,然后使用.append()函数获取所需列表
相关问题 更多 >
编程相关推荐