Parse rfc3339 date strings in Python? [duplicate]
Asked Answered
O

3

103

I have a datasets where all the dates have the following format:

2012-10-09T19:00:55Z

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?

Overdose answered 24/4, 2014 at 18:46 Comment(1)
#969785 duplicate?Villasenor
D
169

You can use dateutil.parser.parse (install with python -m pip install python-dateutil) to parse strings into datetime objects.

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)
Droplet answered 24/4, 2014 at 18:50 Comment(8)
I don't know why I haven't heard of dateutil.parser. These are the little things that make Python awesome. from somemodule import problemsolver && problemsolver.solvemyspecificproblem()Mantua
@Mantua probably because it's not actually part of Python, rather a 3rd-part library. git clone http://example.com/module/problemsolver problemsolver && cd problemsolver && python problemsolver.py myspecificproblemDowntrend
@Droplet My mistake, I did 'pip install dateutil' rather than prepending with python-.Engineering
You need to indicate that thus us a third party libVehicle
@Mantua Python is awesome because it the accepted answer on how to parse a date requires installing a 3rd party library? That seems somewhat less than awesome.Randa
Little catch with parse is dates like this: parse("12/11/2017 07:41:27") datetime.datetime(2017, 12, 11, 7, 41, 27) asumes the first arg is the month parse("13/11/2017 07:41:27") datetime.datetime(2017, 11, 13, 7, 41, 27) magically makes the first arg the day. All explainable just something you need to know.Hygroscopic
@Hygroscopic That gotcha is not ISO-8601 compliant, so it wouldn't be an issue here. The ISO 8601 standard requires a descending arrangement of terms (whether Y-M-D, Y-M, Y-D, Y-W, etc). Though personally I'd prefer that it raise an exception unless some flag is set to automatically convert non-conforming dates.Semitropical
To clarify @Paul's comment, the command to install dateutil with pip is pip install python-dateutil. The "python-" part is required!Mum
H
94

This has already been answered here: How do I translate a ISO 8601 datetime string into a Python datetime object?

d = datetime.datetime.strptime( "2012-10-09T19:00:55Z", "%Y-%m-%dT%H:%M:%SZ" )
d.weekday()
Harhay answered 24/4, 2014 at 18:52 Comment(4)
This is suboptimal, since it requires the caller to provide format strings for the rather wide range of ISO 8601 variants (unless one likes ValueError if the seconds are omitted, e.g). The dateutil parser, on the other hand, handles these much better.Excrescent
@AlexNorth-Keys: How is this suboptimal? The OP stated "I have a datasets where all the dates have the following format" and didn't mention any variants. This answer is actually optimal because it doesn't require any external modules, it uses the built-in datetime module. By using the exact format string (instead of having dateutil guess the format every time), the code performance is optimal.Harhay
I was thinking from a maintenance perspective (oops) - but you're right, the strptime method is faster, running in about 1/6th of the time the dateutil.parser approach does.Excrescent
"%Y-%m-%dT%H:%M:%SZ" produces a “naive” datetime. To produce an “aware” datetime (i.e., aware of the time zone): "%Y-%m-%dT%H:%M:%S%z"Vermouth
G
10

You should have a look at moment which is a python port of the excellent js lib momentjs.

One advantage of it is the support of ISO 8601 strings formats, as well as a generic "% format" :

import moment
time_string='2012-10-09T19:00:55Z'

m = moment.date(time_string, '%Y-%m-%dT%H:%M:%SZ')
print m.format('YYYY-M-D H:M')
print m.weekday

Result:

2012-10-09 19:10
2
Godber answered 7/10, 2014 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.