user3077627
user3077627

Reputation:

Python Date / Time Regular Expression

I am pretty new to regular expressions and it's pretty alien to me. I am parsing an XML feed which produces a date time as follows:

Wed, 23 July 2014 19:25:52 GMT

But I want to split these up so there are as follows:

date = 23/07/2014
time = 19/25/52

Where would I start? I have looked at a couple of other questions on SO and all of them deviate a bit from what I am trying to achieve.

Upvotes: 0

Views: 403

Answers (1)

vaultah
vaultah

Reputation: 46523

Use datetime.strptime to parse the date from string and then format it using the strftime method of datetime objects:

>>> from datetime import datetime
>>> dt = datetime.strptime("Wed, 23 July 2014 19:25:52 GMT", "%a, %d %B %Y %H:%M:%S %Z")
>>> dt.strftime('%d/%m/%Y')
'23/07/2014'
>>> dt.strftime('%H/%M/%S')
'19/25/52'

But if you're okay with the ISO format you can call date and time methods:

>>> str(dt.date())
'2014-07-23'
>>> str(dt.time())
'19:25:52'

Upvotes: 1

Related Questions