Reputation: 1111
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.
Upvotes: 1
Views: 1277
Reputation: 180411
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.
Upvotes: 1