gpt4 book ai didi

python - 使用正则表达式删除字符串末尾不可预测的日期格式

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:28 24 4
gpt4 key购买 nike

我通过以下方式获取字符串,最后以随机模式显示日期。但它只会包含下划线、斜杠、数字或连字符。

TRAVEL_DELAY_01072015
TRAVEL_DELAY_01_07_2015
TRAVEL_DELAY_2015/01/04
TRAVEL_DELAY_2015-01-04

我只需要从上面的字符串中取出 TRAVEL_DELAY。我正在为此使用正则表达式,但不起作用:

m = re.match("^(.*)[_0-9\/.]+", abovestring)

最佳答案

如果您只想拆分日期:

s="""TRAVEL_DELAY_01072015
TRAVEL_DELAY_01_07_2015
TRAVEL_DELAY_2015/01/04
TRAVEL_DELAY_2015-01-04"""

for line in s.splitlines():
date = line.split("_",2)[-1]

01072015
01_07_2015
2015/01/04
2015-01-04

或者str.replace,不需要正则表达式:

for line in s.splitlines():
date = line.replace("TRAVEL_DELAY_","")
print(date)

01072015
01_07_2015
2015/01/04
2015-01-04

如果您实际上是在尝试解析日期,您可以使用 dateutil 并修复字符串:

from dateutil import parser
for line in s.splitlines():
date = line.replace("TRAVEL_DELAY_","")
if any(ch in date for ch in ("/","-","_")):
print(parser.parse(date.replace("_","-")))
else:
date = "{}-{}-{}".format(date[:2],date[2:4],date[4:])
print(parser.parse(date))


2015-01-07 00:00:00
2015-01-07 00:00:00
2015-01-04 00:00:00
2015-01-04 00:00:00

如果数字只在日期中,而您实际上想要的是字符串而不是日期:

 s="""TRAVEL_DELAY_01072015
TRAVEL_DELAY_01_07_2015
TRAVEL_DELAY_2015/01/04
Travel_Delay_Data_2015/01/04
TRAVEL_DELAY_2015-01-04"""

for line in s.splitlines():
ind = next(ind for ind, ele in enumerate(line) if ele.isdigit())
s = line[:ind-1]
print(s)

TRAVEL_DELAY
TRAVEL_DELAY
TRAVEL_DELAY
Travel_Delay_Data
TRAVEL_DELAY

关于python - 使用正则表达式删除字符串末尾不可预测的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28727241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com