1

I use python dateutil to parse string to date:

parse(' 19 2015 '.strip(), fuzzy=True) 

But this throws an error:

 Traceback (most recent call last): File "M:/BitNami_Djangostack/django_mongo_test/glr_test/example.py", line 179, in <module> print('parse(month_to_eng_replace', parse(' 19 2015 ', fuzzy=True)) File "M:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\dateutil\parser.py", line 743, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "M:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\dateutil\parser.py", line 310, in parse ret = default.replace(**repl) ValueError: month must be in 1..12 

In other answer on stackoverflow i read that option fuzzy=True ignores uknown tokens, but how do i explain that 19 is day, not month number, and i provide only two: day and year? option dayfirst=True not working for this.

3
  • You should take a look at strftime.org, you can specify what should be parsed as a day and what should be parsed as a year using it. Commented Aug 24, 2015 at 21:47
  • Is that..day of the year? Or day of the current month? Can you just use the standard time.strptime function (or the datetime equivalent)? Commented Aug 24, 2015 at 21:48
  • what is the month supposed to be? Commented Aug 24, 2015 at 21:50

1 Answer 1

1

If you don't care about the month just add a month that has 31 days and concat that to the start:

from dateutil import parser dte = ' 9 2015 ' d = parser.parse("08" + dte) 

Or just use datetime and split the string:

dte = ' 19 2015 ' from datetime import datetime day, year = dte.split() dte = datetime(day=int(day),year=int(year),month=8) print(dte) 

If you might have a month as commented and it will always be the second part of the string, you can either use that month or use a default value:

from datetime import datetime def parse(s, month=8): data = s.split() return datetime(day=int(data[0]), year=int(data[-1]), month=int(data[-2] if len(data) == 3 else month)) 

It will work for any string with either d-m-y or d-y:

In [10]: parse(" 19 07 2015 ") Out[10]: datetime.datetime(2015, 7, 19, 0, 0) In [11]: parse(" 19 2015 ") Out[11]: datetime.datetime(2015, 8, 19, 0, 0) 

data[-2] will get the second subelement if we have three substrings after splitting, data[0] will get the first and data[-1] will get the last so if the length is not 3 after splitting we only take the first and last elements and use a default value month or we use all three is the length of data is 3.

Sign up to request clarification or add additional context in comments.

5 Comments

this might be correct, but i produce a lot of strings in loop, so i don't know where month is present, and where not. so i need some kind of default current month, but only if it not present in processed string.
will the month always be in the same place if it is there?
I assume in my case the same place - second, after day
This is correct, but i m trying to solve more complex problem: to deal with different mess in date data (you func works with len=2 and 3, but not with 4), for this i use dateutil.readthedocs.org/en/latest/parser.html - and now i need to add additional functionality to this lib. So i prefer to rewrite you function to return string (not datetime) and then pass this string to dateutil parse.
lot of formats - i even dont know all kinds of mess that produce sites, + encoding problems, month may be with characters &#x280 devided with spaces, so there are 5 or 10 items after split

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.