Search code examples
pythonparsingdatetimetimestampstrptime

python: datetime.datetime.strptime error with %z directive


I am having a problem with parsing time string to datetime. A snip of my code is as follows:

 datetime.datetime.strptime('16/Sep/2017:00:00:00 +0000', '%d/%b/%Y:%M:%H:%S %z')

I am getting the following error:

 ValueError: 'z' is a bad directive in format '%d/%b/%Y:%M:%H:%S %z'

I am on Python 2.7.10.

Please come up with simple solution, instead complicated code as I am running this to on a log file having millions of rows. Thanks.

Solution to this particular problem is not found.


Solution

  • Solved with using dateutil module. Here is my solution:

    from dateutil.parser import parse
    parse('16/Sep/2017:00:00:00 +0000'.replace(':', ' ',1))
    datetime.datetime(2015, 6, 24, 0, 1, 3, tzinfo=tzoffset(None, 7200))
    

    There is bug in Python 2.7.* regarding %z directive. Hope they will fix it one day.