1

I'm trying to convert Tue, 30 Sep 2014 16:19:08 -0700 (PDT) into a datetime object. It's currently unicode

This is my first time doing this but it isn't working:

time = 'Tue, 30 Sep 2014 16:19:08 -0700 (PDT)'
date_object = datetime.strptime(time, %a, %d %b %Y %H:%M:%S %X (%Z))

I am getting this error:

  File "quickstart1.py", line 206
    date_object = datetime.strptime(time, %a, %d %b %Y %H:%M:%S %X (%Z))
1

2 Answers 2

1
>>> from dateutil.parser import parse
>>> date_object = parse(time)
Sign up to request clarification or add additional context in comments.

Comments

1

Strings must be strings.

date_object = datetime.strptime(time, '%a, %d %b %Y %H:%M:%S %X (%Z)')

4 Comments

ahh ok. Got this error: sre_constants.error: redefinition of group name 'H' as group 8; was group 5. What is group 8 or 5?
@MorganAllen are you using Python 3?
I thought I was. But when I checked, 2.7.10 (default, Oct 23 2015, 18:05:06) is running. However, I have to do print('test') instead of print test....
So you should use a third party named python-dateutil to parse a date string with timezone information. Maybe this might help: stackoverflow.com/questions/9433851/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.