如何将未知日期格式的字符串转换为Python日期时间对象
我想将此各种未知日期格式的字符串列表转换为python datetime
对象。例如:
strings = ["today", "tomorrow", "next Friday", "June 4th", "04/11/2022"]
convert_to_date(strings[0])
>>> 2022-04-08
convert_to_date(strings[1])
>>> 2022-04-09
convert_to_date(strings[3])
>>> 2022-04-15
我尝试了几种方法,但发现:
dateutil.parser
仅适用于04/11/2022
time> time.strptime.strptime
和<代码>箭头都要求我指定格式REGEX
太复杂了,并且可能无法在所有情况下使用,
是否有任何库或功能可以让我做这样的事情?
I'd like to convert this list of strings of various unknown date formats to a python datetime
object. For example:
strings = ["today", "tomorrow", "next Friday", "June 4th", "04/11/2022"]
convert_to_date(strings[0])
>>> 2022-04-08
convert_to_date(strings[1])
>>> 2022-04-09
convert_to_date(strings[3])
>>> 2022-04-15
I tried several methods but found that:
dateutil.parser
only works for dates like04/11/2022
time.strptime
andarrow
both require me to specify the formatregex
would be too complicated and may not work for all scenarios
Is there any library or function that would allow me to do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有任何图书馆或功能可以做您要的事情。
这里有一些示例代码可以帮助您(在Python中),
我在这里实现了一个基本功能,现在由您决定弄清楚此代码并问自己。
I don't think there is any library or function to do the thing you're asking to.
Here's some sample code to help you out (in python)
I have implemented a basic functionality here, its now up to you to figure this code out and ask yourself.