I have a list of strings in Python 2.7 like this:
lst = [u'Name1_Cap23_o2_A_20160830_20170831_test.tif',
u'Name0_Cap44_o6_B_20150907_20170707.tif',
u'Name99_Vlog_o88_A_20180101_20180305_exten.tif']
What I would like to do is to extract only the string before the two dates so that I get a list like this:
lst = [u'Name1_Cap23_o2_A_20160830_20170831',
u'Name0_Cap44_o6_B_20150907_20170707',
u'Name99_Vlog_o88_A_20180101_20180305']
What I know is how to extract the two dates with re package, but how can I get the list in the example above using datetime and re package. Does anyone have an idea how I could get the rest of the string?
from datetime import datetime
import re
from datetime import datetime
pattern = re.compile(r'(\d{8})_(\d{8})')
dates = pattern.search(lst[0])
startdate = datetime.strptime(dates.group(1), '%Y%m%d')
enddate = datetime.strptime(dates.group(2), '%Y%m%d')
datestring = format(startdate, '%Y%m%d') + "_" + format(startdate, '%Y%m%d')