解析日期字符串? [英] Parse date strings?
本文介绍了解析日期字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,其中所有日期都具有以下格式:
I have a datasets where all the dates have the following format:
2012-10-09T19:00:55Z
我希望能够使用 .weekday等方法
。如何将它们转换成Python中的正确格式?
I'd like to be able to be able to use methods like .weekday
on them. How do I convert them to the proper format in Python?
推荐答案
您可以使用 dateutil.parser.parse
将字符串解析为datetime对象。
You can use dateutil.parser.parse
to parse strings into datetime objects.
dateutil.parser.parse
将尝试猜测字符串的格式,如果您事先知道确切的格式,那么您可以使用 datetime.strptime
格式字符串(见Brent Washburne的答案)。
dateutil.parser.parse
will attempt to guess the format of your string, if you know the exact format in advance then you can use datetime.strptime
which you supply a format string to (see Brent Washburne's answer).
from dateutil.parser import parse
a = "2012-10-09T19:00:55Z"
b = parse(a)
print(b.weekday())
# 1 (equal to a Tuesday)
这篇关于解析日期字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文