Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

Here's how to do it with the string variable:

>>> start_day = 2015188
>>> print start_day
2015188
>>> print conv
time.struct_time(tm_year=2015, tm_mon=7, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=188, tm_isdst=-1)
>>> conv = time.strptime( str(start_day), "%Y%j" )
>>> print conv
time.struct_time(tm_year=2015, tm_mon=7, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=188, tm_isdst=-1)

For whatever reason, you have to put the string variable inside the str() thing and all the examples I have found online only show the date in quotes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top