2

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

6
  • 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. Commented Nov 3, 2016 at 11:54
  • @aquil.abdullah Is there a way for me to check that? Commented Nov 3, 2016 at 12:09
  • @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') Commented Nov 3, 2016 at 12:11
  • 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) Commented Nov 3, 2016 at 12:45
  • @aquil.abdullah Its printing '2 nov 2016' . Can you please tell me how I can remove them? I just started with python Commented Nov 3, 2016 at 13:19

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

10 Comments

The string that I have is in the format "2 nov 2016". Don't think time can handle that conversion :/
"%d %b %Y" read the time module. you'll be surprised at how many things it can handle
Will give it a try. Thanks a lot
the codes are all listed docs.python.org/2/library/time.html#time.strftime and you can just define a date format that suits your needs.
I think its cause of some hidden characters in the string. I am using a scrapped data for the conversion. May be thats got something to do with it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.