简体   繁体   English

Python:从字符串中提取日期并放入列表

[英]Python:Extracting dates from a string and put into a list

I have a string like this:我有一个这样的字符串:

   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

I tried doing like this:我试着这样做:

  #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()

This process works fine, but i want to do only in one operation, rather than doing in two steps(splitting and matching)这个过程工作正常,但我只想在一个操作中完成,而不是分两步进行(拆分和匹配)

  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.

So i will create a list which will contain dates:所以我将创建一个包含日期的列表:

['2016-12-21','2016-12-21', '2016-12-21', '2016-12-21']

With re.findall() , you can achieve that like below:使用re.findall() ,您可以实现如下所示:

re.findall(r'(\d{4}\-\d{2}\-\d{2})', s)

If you want only the second date in each line, try:如果您只想要每行中的第二个日期,请尝试:

re.findall(r':(\d{4}\-\d{2}\-\d{2})', s)

Output:输出:

>>> 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']

尝试先按 \\n 分割,然后您可以逐行迭代并使用反向子字符串获取日期并使用 .append() 函数获取所需的列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM