我有一个日志,输出启动时间的时间戳。我想将每次启动之间的时间与分钟和秒进行比较。
以下是以下列表的格式。
- 16:
- 18:17.10
- 16:20:31.48 = 2 分 14.38 秒 <-- 这就是我想要的要显示的输出。
- 16:27:52.06 = 3 分 20.18 秒 <-- 这就是我想要的要显示的输出。
- 16:33:33.06
- 16:35:50.94
任何帮助将不胜感激。
编辑:
谢谢@smallwat3r
完成的代码是
def split_format(var1, var2):
hour = int(var1.split(':')[0]) - int(var2.split(':')[0])
minute = int(var1.split(':')[1]) - int(var2.split(':')[1])
seconds = float(var1.split (':')[2]) - float(var2.split(':')[2])
time = '{} hours {} minutes {} seconds'.format(
hour, minute, round(seconds, 2)
)
return time
def getFile():
prose = str(input('Please enter the file path for your text file: '))
dictionary = {}
infile = open(prose, 'r')
line_num = 1
line_num2 = 2
for line in infile:
dictionary[line_num]=line
line_num += 1
line_num2 += 1
base = 1
base2 = 2
for x in dictionary:
print(dictionary[base2],(split_format(dictionary[base2],dictionary[base])))
base += 1
base2 += 1
infile.close()
getFile()
你使用下面的返回0 hours 2 minutes 14.38 seconds
time1 = '16:18:17.10'
time2 = '16:20:31.48'
def split_format(var1, var2):
hour = int(var1.split(':')[0]) - int(var2.split(':')[0])
minute = int(var1.split(':')[1]) - int(var2.split(':')[1])
seconds = float(var1.split (':')[2]) - float(var2.split(':')[2])
time = '{} hours {} minutes {} seconds'.format(
hour, minute, round(seconds, 2)
)
return time
print(split_format(time2, time1))