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.
Use
zfillto add those zeros back as needed:By using
i[:-1]we remove that pesky trailing dot, and.zfill(4)will add enough0characters to the left to make it to 4 digits.Demo:
If they are float values instead, use the
format()function on them to give you zero-padded values:So do this:
where
% 2400normalizes your values to the 0. to 2399. range.