为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

胡说叔叔 2022-07-26 21:55:21
我有以下数据集: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。怎么做 ?
查看完整描述

2 回答

?
qq_花开花谢_0

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


查看完整回答
反对 回复 2022-07-26
?
慕哥9229398

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



查看完整回答
反对 回复 2022-07-26
  • 2 回答
  • 0 关注
  • 161 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号