python-3.x - 使用python从日期的左侧和右侧删除字符串
问题描述
string = "I went to market on October 29,2017I"
12-7-2021mma --->12-7-2021
kk12/7/2021 ---> 12-7-2021
yy12/9/2021kko ---> 12/9/2021
是否有任何解决方案可以通过从 2017 年 10 月 29 日删除“我”来获得“我于 2017 年 10 月 29 日上市”I
像上面这样的案例还有很多
解决方案
使用dparser
:
import dateutil.parser as dparser
s = "I went to market on October 29,2017I"
print(dparser.parse(s,fuzzy=True))
输出:
2021-10-29 00:00:00
编辑:
如果您想要特定格式的月份:
s = "I went to market on October 29,2017I"
out = str(dparser.parse(s,fuzzy=True)).split(" ")[0]
print(out)
import datetime
print (datetime.datetime.strptime(out,'%Y-%m-%d').strftime('%B %d, %Y'))
输出:
October 29, 2017
推荐阅读
- swift - 如何在 SwiftUI 中关闭两个视图
- lua - 罗技 g hub 脚本在我发布后立即停止
- r - 使用 rms::Predict 遍历列
- python - 如何使用 PyYaml 或 Ruamel.yaml 仅转储带有 flow_style 的列表
- heroku - 如何从 Azure 发布管道将 React JS 构建文件夹部署到 Heroku 服务器
- python - 尝试使用 openslide 和 py_wsi 时出错
- python - 为什么 float() 不浮动?
- eclipse - 添加新的类路径容器时,原始类路径不会更新
- r - 如何在相同条件下创建新变量的数据集中创建多个新变量并将其添加到数据集中
- c++ - clang 说调用 void consteval 函数不是常量表达式