开发者

Are there existing python modules for dealing with / normalizing units of time represented as strings?

开发者 https://www.devze.com 2023-01-15 13:38 出处:网络
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 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 triv开发者_开发问答ial 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消