2
votes

I am getting an error while trying to parse a string to date.

ValueError: unknown string format

Here is my code

dateString = "02/11/2016"
 print dateString
 dt = parse(dateString)
 item.date = calendar.timegm(dt.utctimetuple())
 print dt

The funny part is, it is printing the correct date before throwing the error. Here is the complete log

02/11/2016 2016-02-11 00:00:00 art. 10, comma 1, lettera e Traceback (most recent call last): File "institutional-docs.py", line 60, in dt = parse(dateString) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py", line 697, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py", line 303, in parse raise ValueError, "unknown string format" ValueError: unknown string format

1
Seems strange it works for me. Did you cut and paste the code from an example? The only thing that I can think of is that maybe there is some type of hidden character in the string. - aquil.abdullah
@aquil.abdullah Is there a way for me to check that? - Kanishka
@aquil.abdullah the data that I am using is coming from a scrapped data. I am escaping the unicode characters. This is what I am doing to ignore them unicodedata.normalize('NFKD', dateString).encode('ascii','ignore') - Kanishka
The easiest way to see if you have any hidden characters left in the string would be to log the results of print repr(dateString) - aquil.abdullah
@aquil.abdullah Its printing '2 nov 2016' . Can you please tell me how I can remove them? I just started with python - Kanishka

1 Answers

2
votes

What's wrong with using time?

your question doesn't specify what your parse function does so can't say if that's reading the string in any weird way. Chances are you've copied and pasted bad quotation marks.

import time
time.strptime("02/11/2016", "%d/%m/%Y")