如何在python中将字符串'2020-01-06T00:00:00.000Z'转换为datetime? [英] How can I convert the string '2020-01-06T00:00:00.000Z' to datetime in python?
本文介绍了如何在python中将字符串'2020-01-06T00:00:00.000Z'转换为datetime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如问题所述,我有一系列字符串,例如‘2020-01-06T00:00:00.000Z’
。如何使用Python将本系列转换为 datetime
?首选熊猫方法。如果没有,有什么方法可以解决这个任务?
As the question says, I have a series of strings like '2020-01-06T00:00:00.000Z'
. How can I convert this series to datetime
using Python? Prefer method on pandas. If not is there any method to solve this task? Thank all.
string '2020-01-06T00:00:00.000Z'
convert to 2020-01-06 00:00:00 under datetime object
下的06 00:00:00
推荐答案
可以通过 datetime.fromisoformat()
:
Python 3.7.6 (default, Dec 19 2019, 22:52:49)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.fromisoformat('2020-01-06T00:00:00.000Z'[:-1])
datetime.datetime(2020, 1, 6, 0, 0)
>>>
将其格式化为%Y-%m-%d%H: %M:%S
,您可以这样做:
To format it as %Y-%m-%d %H:%M:%S
, you can do:
>>> d = datetime.fromisoformat('2020-01-06T00:00:00.000Z'[:-1])
>>> d.strftime('%Y-%m-%d %H:%M:%S')
'2020-01-06 00:00:00'
>>>
这篇关于如何在python中将字符串'2020-01-06T00:00:00.000Z'转换为datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文