Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the replacement for dateutil.parser in python3?

Python 2.x has a great function called dateutil.parser which turns an ISO8601 formatted date into a python datetime value. It's not present in Python 3. What is the replacement?

like image 575
vy32 Avatar asked May 12 '11 03:05

vy32


3 Answers

You should first find the exact name for the module using pip search:

pip search dateutil

Then, install the version you want (assuming py-dateutil):

pip install py-dateutil

Now, fire-up shell and import the module (pitfall: the module is not called py-dateutil):

import dateutil.parser

You should be good to go!

like image 27
EarlyCoder Avatar answered Nov 19 '22 13:11

EarlyCoder


dateutil is a third party module. It has recently been ported to Python 3 with dateutil 2.0, and the parser functions was ported as well.

So the replacement is dateutil.parser. You just forgot to install it.

like image 83
Lennart Regebro Avatar answered Nov 19 '22 14:11

Lennart Regebro


You can achieve this through the datetime module's strptime method.

>>> import datetime
>>> mydate = datetime.datetime(2002,12,4, 12, 30).isoformat()
>>> mydate
'2002-12-04T12:30:00'
>>> parsed_date = datetime.datetime.strptime( mydate, "%Y-%m-%dT%H:%M:%S" )
>>> parsed_date
datetime.datetime(2002, 12, 4, 12, 30)

strptime has a flexible set of options as to parsing your date. See strftime() and strptime() Behavior for more information.

like image 2
onteria_ Avatar answered Nov 19 '22 13:11

onteria_