I am trying to simply create a datetime object from the following date: 'Fri Mar 11 15:59:57 EST 2016' using the format: '%a %b %d %H:%M:%S %Z %Y'.
Here's the code.
from datetime import datetime
date = datetime.strptime('Fri Mar 11 15:59:57 EST 2016', '%a %b %d %H:%M:%S %Z %Y')
However, this results in a ValueError as shown below.
ValueError: time data 'Fri Mar 11 15:59:57 EST 2016' does not match format '%a %b %d %H:%M:%S %Z %Y'
Maybe I am missing something obviously wrong with my format string, but I have checked it over and over. Any help would be greatly appreciated, thanks.
Edit to reflect comments/questions for more information:
The Python version I'm using is 2.7.6.
Using the 'locale' command on Ubuntu 14.04 I get this:
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
For the %Z specifier, strptime
only recognises "UTC", "GMT" and whatever is in time.tzname
(so whether it recognises "EST" will depend on the time zone of your computer). This is issue 22377.
See:
Python strptime() and timezones?
Parsing date/time string with timezone abbreviated name in Python?
The best option for parsing timezones that include a human-readable time zone is still to use the third-party python-dateutil library:
import dateutil
date = dateutil.parse('Fri Mar 11 15:59:57 EST 2016')
If you cannot install python-dateutil, you could strip out the timezone and parse it manually e.g. using a dictionary lookup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With