I want to extract a date
from a string
using python's dateutil
package. The date
comes in different formats, but the day
part of the date
is not present in any one of these string
.
Month
when written in Alphabets preceeds year
like Sep 2016
, but while written as numeric succeeds year
like 2016-09
or 201609
import dateutil.parser as dparser
print(dparser.parse("The file is for month Sep 2016.",fuzzy=True).month)
9
print(dparser.parse("The file is for month Sept-2016.",fuzzy=True).month)
9
print(dparser.parse("The file is for month 2016-09.",fuzzy=True).month)
9
How to deal with the case when there is no hyphen -
between year
and month
as shown below -
print(dparser.parse("The file is for month 201609.",fuzzy=True).month)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-123-566e083c8313> in <module>()
----> 1 dparser.parse("The file is for month 201609.",fuzzy=True).month
~\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser.py in parse(timestr, parserinfo, **kwargs)
1180 return parser(parserinfo).parse(timestr, **kwargs)
1181 else:
-> 1182 return DEFAULTPARSER.parse(timestr, **kwargs)
1183
1184
Is there an option inside this library to do so?