Search code examples
pythondatetimestrptime

Python - strptime ValueError: time data does not match format '%Y/%m/%d'


I believe I am missing something trivial. After reading all the questions about strptime ValueError yet I feel the format seems right, Here is the below error I get

Traceback (most recent call last):
  File "loadScrip.py", line 18, in <module>
    nextDate = datetime.datetime.strptime(date, "%Y/%m/%d")
  File "/usr/lib64/python2.6/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '20l2/08/25' does not match format '%Y/%m/%d'

I am using Python 2.6.6 under Linux x86_64. Any help will be much appreciated.


Solution

  • Your error indicates you have data with the letter l (lowercase L) instead of the number 1 in the year:

    ValueError: time data '20l2/08/25' does not match format '%Y/%m/%d'
    

    That is not a valid date that'll fit the requested format; replacing the l with 1 and the input date works just fine:

    >>> import datetime
    >>> datetime.datetime.strptime('20l2/08/25', "%Y/%m/%d")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python2.7/_strptime.py", line 325, in _strptime
        (data_string, format))
    ValueError: time data '20l2/08/25' does not match format '%Y/%m/%d'
    >>> datetime.datetime.strptime('2012/08/25', "%Y/%m/%d")
    datetime.datetime(2012, 8, 25, 0, 0)
    

    Fix your input, the format is correct.