Search code examples
pythondatestrptimerfc3339

How to parse e.g. 2010-04-24T07:47:00.007+02:00 with Python strptime


Does anyone know how to parse the format as described in the title using Pythons strptime method?

I have something similar to this:

import datetime    
date = datetime.datetime.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.Z')

I can't seem to figure out what kind of timeformat this is. By the way, I'm a newbie at the Python language (I'm used to C#).

UPDATE

This is how I changed the code based on the advise (answers) below:

from dateutil.parser import *
from datetime import *
date = parse(entry.published.text)

Solution

  • That date is in ISO 8601, or more specifically RFC 3339, format.

    Such dates can't be parsed with strptime. There's a Python issue that discusses this.

    dateutil.parser.parse can handle a wide variety of dates, including the one in your example.

    If you're using an external module for XML or RSS parsing, there is probably a routine in there to parse that date.