在Python中解析rfc3339日期字符串? [英] Parse rfc3339 date strings in Python?

查看:585
本文介绍了在Python中解析rfc3339日期字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中所有日期都具有以下格式:

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 来将字符串解析为日期时间对象.

You can use dateutil.parser.parse to parse strings into datetime objects.

dateutil.parser.parse将尝试猜测字符串的格式,如果您事先知道确切的格式,则可以使用

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)

这篇关于在Python中解析rfc3339日期字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆