Parse clock without pointers to strptime in Python
Let's assume you have a time in this format:
a = [..., 800.0, 830.0, 900.0, 930.0, 1000.0, 1030.0, ...]
The problem is that there are no leading zeros for hours. For example, 00:30
presented 30
, 08:00
presented 800.
, and 00:00
presented 2400
. Is it possible to parse this data into an object time
using a method strptime
? I tried using the following code
hours = [time.strptime(str(int(i)), "%H%M") for i in a]
but got
ValueError: unconverted data remains: 0
PS I am using Python 2.7.
source to share
Use zfill
to add these zeros back as needed:
hours = [time.strptime(i[:-1].zfill(4), "%H%M") for i in a]
Using i[:-1]
, we'll remove that tightened endpoint and .zfill(4)
add enough characters to the 0
left to make it up 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 floating point values, use format()
function on them to get null values:
>>> format(800., '04.0f')
'0800'
Do this:
hours = [time.strptime(format(i % 2400, '04.0f'), "%H%M") for i in a]
where will % 2400
normalize your values to the range 0 to 2399.
source to share
In this case, you can allocate hours, minutes without strptime()
:
>>> 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 any reason; you can get the format you want with x % y
:
>>> ["%04.0f" % (f % 2400) for f in a]
['0800', '0830', '0900', '0930', '1000', '1030', '0030', '0000']
source to share
If it's really floating literals in there, and 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]
This will round off the decimal if any ( 1033.66666
will 1034
therefore become 10:34 AM
)
You can also truncate like this:
[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 comments
If you have out of range values, 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 do your original code as well:
[time.strptime(i,'%H%M') for i in[str(int(f)) if f<2400 else '0000' for f in a]]
source to share