简体   繁体   中英

Time formatting strings in Python

I'm trying to convert these strings of numbers into a "hh:mm:ss" format. The strings are all different lengths, here are a few:

(212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)

So I want to turn the above numbers into this:

(21:28:12, 21:86:54, 23:25:27, 23:59:59, 00:00:00, 18:12:40, 2:59:59, 15:38:34)

I'm mostly having trouble with getting them all the same length, like converting 0 to 00:00:00.

Thanks!

As others have pointed out in comments, it's unclear what the right answer is for some inputs (eg 1234, which my code would say is 00:12:34). I also decided that 02:59:59 is a better answer than 2:59:59, given the desire to get 00:00:00.

So here's my code, which deals with all of the above inputs correctly, modulo the 2:59:59 variation I chose:

import re

def convert(numeric_time):
    return ':'.join(re.findall('..', str(numeric_time).zfill(6)))

times = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
correct_answers = ['21:28:12', '21:86:54', '23:25:27', '23:59:59', '00:00:00', '18:12:40', '02:59:59', '15:38:34']

answers = map(convert, times)
for answer, correct_answer in zip(answers, correct_answers):
    assert answer == correct_answer, '{} != {}'.format(answer, correct_answer)

UPDATE

Since some people object to the regular expression, here's a similar version that doesn't rely on it:

def convert(numeric_time):
    padded_time = str(numeric_time).zfill(6)
    return ':'.join(padded_time[i:i+2] for i in range(0, len(padded_time), 2))

Well since we're making up answers here's a "solution" that doesn't use regexes:

In [3]: def weird_time_format(fmt):
   ...:     fmt = str(fmt)
   ...:     hours = fmt[:2].ljust(2, '0')
   ...:     mins = fmt[2:4].ljust(2, '0')
   ...:     secs = fmt[4:6].ljust(2, '0')
   ...:     return ':'.join((hours, mins, secs))
   ...:

In [4]: weird_time_format(212812)
Out[4]: '21:28:12'

This takes advantage of the fact that string slices are nice about out-of-bound indexes and return an empty string rather than throwing an error:

In [1]: ''[1:2]
Out[1]: ''

In [2]: ''[1:2].ljust(2, '0')
Out[2]: '00'

Here's the results for your example input:

In [5]: example_input = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)

In [6]: tuple(map(weird_time_format, example_input))
Out[6]:
('21:28:12',
 '21:86:54',
 '23:25:27',
 '23:59:59',
 '00:00:00',
 '18:12:40',
 '25:95:90',
 '15:38:34')

And since I brought it up, what it does to 1234 :

In [7]: weird_time_format(1234)
Out[7]: '12:34:00'

OK, I felt (a little) bad for being facetious. If you're genuinely interested in this approach, this will work better and is more in line with the other answer's output:

In [3]: def weird_time_format(fmt):
   ...:     fmt = str(fmt).rjust(6, '0')
   ...:     return ':'.join((fmt[:2], fmt[2:4], fmt[4:6]))

Here is another attempt.

The basic concept is adding 0 if input string length less than 6 .

a = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
input_list = map(str,a)
for input in input_list:
    if len(input) != 6:
        input = ''.join(['0' for i in range((6 - len(input)))]+list(input))
    print ':'.join([input[i:i+chunk_size] for i in range(0,len(input),len(input)/3)])  

Result

21:28:12
21:86:54
23:25:27
23:59:59
00:00:00
18:12:40
02:59:59
15:38:34

Hope this helps:

data = (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
time = map(str, data) #int to string


def conv(time_array):
    for t in time_array:
        if(len(t) != 6):
            t = str(t.zfill(6)) #filling 0's if less than 6
        print ':'.join(t[i:i + 2] for i in range(0, len(t), 2)) #adding ':' 

conv(time)

OUTPUT Using some test data

test = (1234, 23, 133, 6)
00:12:34
00:00:23
00:01:33
00:00:06

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM