views:

29

answers:

1

I am new to python and am doing an export and import to a new db.

I have a column on my export (to be imported) of strings for units of time, "20 minutes" "1.5 hours" "2 1/2 hours", etc.

I tried googling but couldn't really find any good phrases and kept coming up with information more related to datetime units rather than just units of time.

Fairly trivial to implement, but it seems like there is a good chance something exists.

Basically what I want to do is have the new DB just have the time in minutes, so I need to turn "1.5 hours" and "2 1/2 hours" into 90 and 150, respectively.

+1  A: 

For the first two formats it looks like you can use the excellent 3rd-party module dateutil, e.g.:

>>> from dateutil import parser
>>> dt = parser.parse('1.5 hours') # returns `datetime` object
>>> t = dt.time()
>>> t
datetime.time(1, 30)

This doesn't appear to work for "2 1/2 hours", however.

Adam Bernier