我有一个这样的字符串:
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']
答案 0 :(得分:1)
使用re.findall()
,您可以实现以下目标:
re.findall(r'(\d{4}\-\d{2}\-\d{2})', s)
如果您只需要每行中的第二个日期,请尝试:
re.findall(r':(\d{4}\-\d{2}\-\d{2})', s)
<强>输出:强>
>>> import re
>>>
>>> s = '''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'''
>>>
>>> re.findall(r':(\d{4}\-\d{2}\-\d{2})', s)
['2016-12-21', '2016-12-21']
答案 1 :(得分:0)
首先尝试按\ n分割,然后你可以逐行迭代并使用反向子字符串获取日期并使用.append()函数获取所需的列表