У меня есть вложенный список с форматом времени AM, PM. Я хочу преобразовать их в 24-часовой формат. Я написал код, который преобразует время в 24, но результат не соответствует действительности. Что не так в моем коде?
mylist = [
['x', '06:00 - 09:30 AM - 10:30 AM , 02:00 PM - 05:00 PM - 09:00 PM'],
['y', '07:30 AM - 02:30 PM, 07:30 AM , 02:30 PM, 07:30 AM - 01:30 PM']
]
correctResult= [['x', '06:00 - 09:30 AM - 10:30 AM - 14:00 PM - 17:00 PM - 21:00 PM'],
['y', '07:30 AM - 14:30 PM, 07:30 AM - 14:30 PM, 07:30 AM - 13:30 PM']]
myresult = [
['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']]
]
Код следующий:
def time24(timepmam):
from datetime import datetime
in_time = datetime.strptime(timepmam, "%I:%M %p")
out_time = datetime.strftime(in_time, "%H:%M")
return out_time
result = []
for x in mylist:
res = x[0]
ss = []
for y in x[1].split(' - '):
# print(y)
if len(y) > 5 and len(y)<9:
print(y)
t = time24(y)
ss.append(t)
else:
ss.append(y)
s = ' '.join(ss)
result.append([res, ss])
print(result)