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

Kanishka
  • 1,097
  • 5
  • 20
  • 37
  • 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 Nov 03 '16 at 11:54
  • @aquil.abdullah Is there a way for me to check that? – Kanishka Nov 03 '16 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')` – Kanishka Nov 03 '16 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)``` – aquil.abdullah Nov 03 '16 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 – Kanishka Nov 03 '16 at 13:19
  • Refer this http://stackoverflow.com/questions/466345/converting-string-into-datetime – Piyush Gupta Nov 21 '16 at 12:18

1 Answers1

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")
Tom
  • 343
  • 1
  • 13
  • The string that I have is in the format "2 nov 2016". Don't think time can handle that conversion :/ – Kanishka Nov 03 '16 at 12:08
  • `"%d %b %Y"` read the time module. you'll be surprised at how many things it can handle – Tom Nov 03 '16 at 12:13
  • Will give it a try. Thanks a lot – Kanishka Nov 03 '16 at 12:14
  • the codes are all listed https://docs.python.org/2/library/time.html#time.strftime and you can just define a date format that suits your needs. – Tom Nov 03 '16 at 12:15
  • The same strange thing is happening again. Its able to identify and convert the string. But in the end, it still throws error saying its not in a valid format. here is the log – Kanishka Nov 03 '16 at 12:21
  • String: 2 nov 2016 log: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=307, tm_isdst=-1) Error: ValueError: time data 'art. 10, comma 1, lettera e ' does not match format '%d %b %Y' – Kanishka Nov 03 '16 at 12:21
  • 1
    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. – Kanishka Nov 03 '16 at 12:27
  • ` Error: ValueError: time data 'art. 10, comma 1, lettera e ' does not match format '%d %b %Y' ` is saying you are trying to convert the string: `'art. 10, comma 1, lettera e '` which does not seem like a date format to me. So something is wrong with you extracting time values. – Tom Nov 03 '16 at 12:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127282/discussion-between-kanishka-and-tom). – Kanishka Nov 03 '16 at 13:16
  • ValueError: unconverted data remains: – CS QGB Oct 29 '20 at 13:38