2 回答

TA贡献1835条经验 获得超7个赞
date = string.split("AVE_")[-1].split(".csv")[0]
解释
.split("AVE_")[-1] # will take the last entry so if there is no "AVE" in front it will just leave the string as it is.
".split(".csv")[0]" # The last part strips away .csv if it exists otherwise leaves the string unchanged
输出
>>> my_list
['AVE_2020_01_13', 'AVE_2020_01_15', 'AVE_2020_01_13', 'AVE_2020_02_10',
'AVE_2020_02_10', 'AVE_2020_02_10', '2020_01_29.csv', '2019_12_02.csv']
>>> my_new_list = []
>>> for entry in my_list:
... my_new_list.append(entry.split("AVE_")[-1].split(".csv")[0])
...
>>> my_new_list
['2020_01_13', '2020_01_15', '2020_01_13', '2020_02_10', '2020_02_10',
'2020_02_10', '2020_01_29', '2019_12_02']

TA贡献1877条经验 获得超6个赞
import re
result = re.findall(r'\d+[\d_]+', 'AVE_2020_01_13 AVE_2020_01_15 AVE_2020_01_13
AVE_2020_02_10 AVE_2020_02_10 AVE_2020_02_10 2020_01_29.csv 2019_12_02.csv')
print(result) # ['2020_01_13', '2020_01_15', '2020_01_13', '2020_02_10', '2020_02_10', '2020_02_10', '2020_01_29', '2019_12_02']
添加回答
举报