将字符串列表与包含文件的路径进行比较



我有一个文件路径列表:

rasterfiles= glob.glob(r'Y:5_Wettersystemecmwf_datatemperature**avg.tif')
rasterfiles = ['Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000291_temperature_avg.tif, 'Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000225_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000027_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000254_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000177_temperature_avg.tif,...']

和列表:

def dates_to_dayoftheyears(start, end):
duration = end - start
period = []
for d in range(duration.days + 1):
day = start + timedelta(days=d)
period.append(day)
year = []
for day in period:
year.append(day.strftime('%Y'))
dayoftheyear = []
for day in period:
dayoftheyear.append(day.strftime('%j'))
year_and_days = [a+ b for a,b in zip(year, dayoftheyear)]
return year_and_days
print (dates_to_dayoftheyears(date(2013,4,1), date(2013,4,30)))

输出

['2013091', '2013092', '2013093', '2013094', '2013095', '2013096', '2013097', '2013098', '2013099', '2013100', '2013101', '2013102', '2013103', '2013104', '2013105', '2013106', '2013107', '2013108', '2013109', '2013110', '2013111', '2013112', '2013113', '2013114', '2013115', '2013116', '2013117', '2013118', '2013119', '2013120']

如何仅提取rasterfiles列表中与第二个列表具有相同字符串序列的文件?

您可以简单地迭代值

import os
rasterfiles = ['Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000291_temperature_avg.tif', 'Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000225_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000027_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000254_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000177_temperature_avg.tif']
dates = ['2013091', '2013092', '2013093', '2013094', '2013095', '2013096', '2013097', '2013098', '2013099', '2013100', '2013101', '2013102', '2013103', '2013104', '2013105', '2013106', '2013107', '2013108', '2013109', '2013110', '2013111', '2013112', '2013113', '2013114', '2013115', '2013116', '2013117', '2013118', '2013119', '2013120']
result = []
for f in rasterfiles:
for d in dates:
if d in os.path.basename(f):
result.append(f)
print(result)

在更复杂的版本中,您可以使用正则表达式提取文件名的日期部分,并使用它与日期列表进行比较:

import os
import re
rasterfiles = ['Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000291_temperature_avg.tif', 'Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000225_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000027_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000254_temperature_avg.tif','Y:\5_Wetter\system\ecmwf_data\temperature\2000\ecmwf_2000177_temperature_avg.tif']
dates = ['2013091', '2013092', '2013093', '2013094', '2013095', '2013096', '2013097', '2013098', '2013099', '2013100', '2013101', '2013102', '2013103', '2013104', '2013105', '2013106', '2013107', '2013108', '2013109', '2013110', '2013111', '2013112', '2013113', '2013114', '2013115', '2013116', '2013117', '2013118', '2013119', '2013120']
re_pattern = "[a-zA-Z]+_([0-9]{6,8})_.*"
result = []
for f in rasterfiles:
m = re.search(re_pattern, os.path.basename(f))
if (m != None):
if m.group(1) in dates:
result.append(f)
print(result)

相关内容

  • 没有找到相关文章

最新更新