Python - strptime ValueError: los datos de tiempo no coinciden con el formato '%Y/%m/%d'
Frecuentes
Visto 19,535 veces
2
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.
2 Respuestas
9
Your error indicates you have data with the letter l
(lowercase L) instead of the number 1
en el año:
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
con 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.
Respondido el 09 de Septiembre de 13 a las 22:09
-1
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.
Respondido el 25 de junio de 15 a las 19:06
Necesita str
in your case because you're starting with an integer - this doesn't solve the OP's problem - Jonrsharpe
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python datetime strptime or haz tu propia pregunta.
@Martjin : Great answer, one question please, how can I read a date format from a file as datetime, I mean I have a date 2016-06-21 and I want to read it as 2016-06-21 00:00:00 because the format could be date or datetime and I should read it as datetime always. Thanks. - Jad Chahiné
@JadChahine: exactly as described here; use
datetime.strptime()
en esas cuerdas. - Martijn Pietersyes but if the string is a datetime and I use %Y/%m/%d then I got a error that the time date does not match the format. - Jad Chahiné
Well, then you have a string that can't be parsed by that specific format string. Make sure you have strings that do match, or adjust your format string. This includes newline characters or other whitespace, by the way. - Martijn Pieters