猿问

如何在python中提取数字和下划线

我有以下数据集:

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

我需要提取 2019_12_02。怎么做 ?


胡说叔叔
浏览 162回答 2
2回答

qq_花开花谢_0

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

慕哥9229398

import reresult = re.findall(r'\d+[\d_]+', 'AVE_2020_01_13 AVE_2020_01_15 AVE_2020_01_13AVE_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']
随时随地看视频慕课网APP

相关分类

Python
我要回答