Python中的时间格式字符串

Python中的时间格式字符串,python,datetime,string-formatting,Python,Datetime,String Formatting,我正在尝试将这些数字字符串转换为“hh:mm:ss”格式。字符串的长度都不同,以下是一些: (212812, 218654, 232527, 235959, 0, 181240, 25959, 153834) 所以我想把上面的数字变成: (21:28:12, 21:86:54, 23:25:27, 23:59:59, 00:00:00, 18:12:40, 2:59:59, 15:38:34) 我很难让它们都保持相同的长度,比如将0转换为00:00:00 谢谢 正如其他人在评论中指出的,对于

我正在尝试将这些数字字符串转换为“hh:mm:ss”格式。字符串的长度都不同,以下是一些:

(212812, 218654, 232527, 235959, 0, 181240, 25959, 153834)
所以我想把上面的数字变成:

(21:28:12, 21:86:54, 23:25:27, 23:59:59, 00:00:00, 18:12:40, 2:59:59, 15:38:34)
我很难让它们都保持相同的长度,比如将0转换为00:00:00


谢谢

正如其他人在评论中指出的,对于某些输入(例如1234,我的代码是00:12:34),不清楚正确答案是什么。我还认为02:59:59比2:59:59更好,因为我想得到00:00:00

这是我的代码,它正确地处理了上面所有的输入,对我选择的2:59:59变量进行模化:

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)

更新

由于有些人反对正则表达式,这里有一个类似的版本不依赖它:

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))

正如其他人在评论中指出的,对于某些输入(例如1234,我的代码是00:12:34),不清楚正确答案是什么。我还认为02:59:59比2:59:59更好,因为我想得到00:00:00

这是我的代码,它正确地处理了上面所有的输入,对我选择的2:59:59变量进行模化:

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)

更新

由于有些人反对正则表达式,这里有一个类似的版本不依赖它:

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))

既然我们在编答案,这里有一个不使用正则表达式的“解决方案”:

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'
这充分利用了以下事实,即字符串切片对于超出绑定的索引很好,并返回一个空字符串,而不是抛出一个错误:

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

In [2]: ''[1:2].ljust(2, '0')
Out[2]: '00'
以下是示例输入的结果:

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')
自从我提起它,它对1234的作用是什么:

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

好吧,我觉得开玩笑(有点)不好。如果您真的对这种方法感兴趣,那么这种方法会更好,并且更符合另一个答案的输出:

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

既然我们在编答案,这里有一个不使用正则表达式的“解决方案”:

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'
这充分利用了以下事实,即字符串切片对于超出绑定的索引很好,并返回一个空字符串,而不是抛出一个错误:

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

In [2]: ''[1:2].ljust(2, '0')
Out[2]: '00'
以下是示例输入的结果:

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')
自从我提起它,它对1234的作用是什么:

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

好吧,我觉得开玩笑(有点)不好。如果您真的对这种方法感兴趣,那么这种方法会更好,并且更符合另一个答案的输出:

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

这是另一种尝试

基本概念是,如果输入字符串长度小于
6
,则添加
0

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)])  
结果

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

这是另一种尝试

基本概念是,如果输入字符串长度小于
6
,则添加
0

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)])  
结果

21:28:12
21:86:54
23:25:27
23:59:59
00:00:00
18:12:40
02:59:59
15:38:34
希望这有助于:

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)
输出 使用一些测试数据

test = (1234, 23, 133, 6)
00:12:34
00:00:23
00:01:33
00:00:06
希望这有助于:

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)
输出 使用一些测试数据

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

由于您正在(或您认识的人正在)编写自己的时间序列化格式,如果我们想帮助您反序列化它,您可以向我们解释一下吗?什么时候是
1234
?为什么
0
变成
00:00:00
,而
25959
只变成
2:59:59
,而不是
02:59:59
?既然你在编(或者你认识的人在编)你自己的时间序列化格式,如果我们想帮你反序列化,也许你可以向我们解释一下?
1234
是几点?为什么
0
变成
00:00:00
,而
25959
只变成
2:59:59
而不是
02:59:59
?几点了?“25点95分,我的手表。”你为什么要把零放在右边而不是左边呢?因为这样你就可以享受到有趣的时光,比如
25:95:90
,再加上示例中的
21:86:54
。哦,我明白了。采取更加愤世嫉俗的方法。:-)几点了?“25点95分,我的手表。”你为什么要把零放在右边而不是左边呢?因为这样你就可以享受到有趣的时光,比如
25:95:90
,再加上示例中的
21:86:54
。哦,我明白了。采取更加愤世嫉俗的方法。:-)