Parse hours without leading zeroes by strptime in Python

走远了吗. 提交于 2019-12-02 00:07:14

问题


Suppose you have time in this format:

a = [..., 800.0, 830.0, 900.0, 930.0, 1000.0, 1030.0, ...]

The problem is that leading zeroes for hours are missing. For example 00:30 is represented by 30, 08:00 is represented by 800. and 00:00 is represented by 2400. Is it possible to parse this data to time object using strptime method? I tried using following code

hours = [time.strptime(str(int(i)), "%H%M") for i in a]

but got

ValueError: unconverted data remains: 0

P.S. I'm using Python 2.7.


回答1:


Use zfill to add those zeros back as needed:

hours = [time.strptime(i[:-1].zfill(4), "%H%M") for i in a]

By using i[:-1] we remove that pesky trailing dot, and .zfill(4) will add enough 0 characters to the left to make it to 4 digits.

Demo:

>>> import time
>>> a = ['800.', '830.', '900.', '30.']
>>> [time.strptime(i[:-1].zfill(4), "%H%M") for i in a]
[time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=9, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)]

If they are float values instead, use the format() function on them to give you zero-padded values:

>>> format(800., '04.0f')
'0800'

So do this:

hours = [time.strptime(format(i % 2400, '04.0f'), "%H%M") for i in a]

where % 2400 normalizes your values to the 0. to 2399. range.




回答2:


You can extract hours, minutes without strptime() in this case:

>>> from datetime import time
>>> a = [800., 830., 900., 930., 1000., 1030., 30., 2400.]
>>> [time(*divmod(int(f) % 2400, 100)) for f in a]
[datetime.time(8, 0), 
 datetime.time(8, 30), 
 datetime.time(9, 0), 
 datetime.time(9, 30),
 datetime.time(10, 0),
 datetime.time(10, 30),
 datetime.time(0, 30),
 datetime.time(0, 0)]

If you want to use strptime() for whatever reason; you could concisely get the required format using x % y:

>>> ["%04.0f" % (f % 2400) for f in a]
['0800', '0830', '0900', '0930', '1000', '1030', '0030', '0000']



回答3:


If those are really float literals in there vs strings, you can do this:

a=[800., 830., 900., 930., 1000., 1030.]
hours=[time.strptime('{:04.0f}'.format(f), '%H%M') for f in a]

That will round the decimal if any (1033.66666 would be 1034 hence becoming 10:34 AM)

You can also truncate like so:

[800.0, 830.0, 900.0, 930.0, 1000.0, 1030.0, 1033.3333333, 1033.66666]
hours=[time.strptime(str(f).split('.')[0], '%H%M') for f in a]

edit from comments

If you have values outside the range, you do this:

a=[800., 830., 900., 930., 1000., 1030., 2400.]
hours=[time.strptime(s,'%H%M') for s in ['{:04.0f}'.format(f) if f <2400 else '0000' for f in a]]

or, you can make your original code work that way as well:

[time.strptime(i,'%H%M') for i in[str(int(f)) if f<2400 else '0000' for f in a]]


来源:https://stackoverflow.com/questions/14735026/parse-hours-without-leading-zeroes-by-strptime-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!