The date in my data does not come in one single format. It comes as "yyyy", "mm/dd/yyyy" and "Jun-03". I want to extract the year out of it.
2 Answers
Using dateutil:
In [24]: import dateutil.parser as parser
In [28]: parser.parse('Jun-03').year # assumes the current year
Out[28]: 2013
In [29]: parser.parse('08/09/2012').year
Out[29]: 2012
In [30]: parser.parse('2012').year
Out[30]: 2012
4 Comments
mickey
I have installed the python-dateutil(2.1 version) but it would throw No module found. >>> import dateutil Traceback (most recent call last): File "<pyshell#101>", line 1, in <module> import dateutil ImportError: No module named dateutil
unutbu
How did you install
python-dateutil? The message ImportError: No module named dateutil indicates it was not successfully installed.mickey
Downloaded the tar.zip file, unzipped it and from the command prompt went into the folder and typed 'python setup.py install' . I can see "python_dateutil-2.1-py2.7.egg" folder only in the site packages
Avraham Zhurba
@mickey To install the package go to Scripts folder under Python folder and run the following:
C:\Python27\Scripts>pip install python-dateutil