【问题标题】:How would one write a regex command to convert string to datetime format in python?如何编写一个正则表达式命令在 python 中将字符串转换为日期时间格式?
【发布时间】:2019-10-24 15:41:38
【问题描述】:

如何编写正则表达式命令将“yyyy-mm-ddXhh:mmY[GMT]”转换为 python 中的有效日期时间格式?

我是正则表达式的新手并尝试了以下方法,但没有成功

df[df.columns['a']] = df[df.columns['a']].str.replace(r'[^0-9]+-[^0-9]+-[^0-9]+[A-Z][^0-9]+:[^0-9]+[A-Z][][A-Z]+[]', "")

【问题讨论】:

  • 你可能需要pd.to_datetime
  • 你试过pd.to_datetime()

标签: python regex pandas data-cleaning


【解决方案1】:

希望这会有所帮助

import re
from datetime import datetime

str = "2019-06-09X14:38Y[GMT]"
y = re.split("Y", str)
x = re.split("X", y[0])

datetime_object = datetime.strptime(x[0]+' '+x[1], '%Y-%m-%d %H:%M')
print(datetime_object)

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 2014-08-10
    • 2015-11-19
    相关资源
    最近更新 更多