[英]How to convert a string of date and time in words to numbers?
I have the following string: January 1, 2020 / 12:04 AM / a month ago
.我有以下字符串: January 1, 2020 / 12:04 AM / a month ago
。 How do I convert this into 1/1/2020 0:04:00
?如何将其转换为1/1/2020 0:04:00
? The code should ignore the a month ago
.代码应该忽略a month ago
. How do I do this?我该怎么做呢?
You could use dateutil
.您可以使用dateutil
。 It is a third part extension of datetime module.它是 datetime 模块的第三部分扩展。 You can add it with python -m pip install python-dateutil
您可以使用python -m pip install python-dateutil
添加它
from dateutil.parser import parse
data = 'January 1, 2020 / 12:04 AM / a month ago'
resp = parse(data, fuzzy_with_tokens=True)
print(resp[0]) # the first index is datetime object
The parser is relatively powerful.解析器比较强大。 Here is documentation to Parser .这是Parser 的文档。
dateutil is one among many that can help you solve your problem. dateutil 是可以帮助您解决问题的众多工具之一。 Good summary of tools such as maya, arrow etc are found Stackabuse thanks to @WasabiMonster感谢@ WasabiMonster ,Stackabuse 找到了对Maya 、Arrow 等工具的很好的总结
using strptime
you can do it and below is the sample,i was using in my code使用strptime
你可以做到,下面是示例,我在我的代码中使用
datetime.datetime.strptime(
... "April 18, 2019, 09:09:09", "%B %d, %Y, %H:%M:%S"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.