简体   繁体   English

如何在 Python 中将输出从秒转换为 hhmmss

[英]How to convert output from seconds into hhmmss in Python

I am completely new to Python and need help to convert my output from seconds to hhmmss format.我对 Python 完全陌生,需要帮助将我的输出从几秒钟转换为 hhmmss 格式。 So instead of the output saying 182.79569892473117 seconds, I would like the output to be 0 hours, 3 minutes, 3 seconds.因此,我希望输出为 0 小时 3 分 3 秒,而不是输出 182.79569892473117 秒。

def main():定义主():

#closest distance #最近距离

c = 34000000

#farthest distance #最远距离

f = 249000000

#average distance #平均距离

a = 139000000

#time = distance/speed #time = 距离/速度

closest = c / 186000*1
farthest = f / 186000*1
average = a / 186000*1

print("The photo will take ",closest,"seconds to reach home.")
print("The photo will take ",farthest," seconds to reach home.")
print("The photo will take ",average," seconds to reach home.")

main()主要的()

import datetime

a = datetime.timedelta(seconds=182.79569892473117)
print(a)

you can use the time module to do this.你可以使用time模块来做到这一点。 this answer will help 这个答案会有所帮助

here is an example这是一个例子

import time 
print(time.strftime("%H hours %M minutes %S secs",time.gmtime(186)))

Here is an example of the calculation and also how you would format the result.这是一个计算示例以及如何格式化结果。

def gethms(secs):
    h = int(secs / 3600)
    secs -= h * 3600
    m = int(secs / 60)
    secs -= m * 60
    return h, m, secs

def format_seconds(secs):
    h, m, s = gethms(secs)
    return f"{h:d}:{m:02d}:{s:05.2f}"


def main():

    #closest distance
    c = 34000000
    
    #farthest distance
    f = 249000000

    #average distance
    a = 139000000

    #time = distance/speed
    closest = c / 186000*1
    farthest = f / 186000*1
    average = a / 186000*1

    print("The photo will take",format_seconds(closest),
          "to reach home.")

    print("The photo will take",format_seconds(farthest),
          "to reach home.")

    print("The photo will take",format_seconds(average),
          "to reach home.")

main()

Which gives:这使:

The photo will take 0:03:02.80 to reach home.
The photo will take 0:22:18.71 to reach home.
The photo will take 0:12:27.31 to reach home.

Or if as the format string in the format_seconds function you use:或者,如果您使用format_seconds函数中的格式字符串:

    return f"{h} hours, {m} minutes, {round(s)} seconds"

then you will get:那么你会得到:

The photo will take 0 hours, 3 minutes, 3 seconds to reach home.
The photo will take 0 hours, 22 minutes, 19 seconds to reach home.
The photo will take 0 hours, 12 minutes, 27 seconds to reach home.
c = 34000000
f = 249000000
a = 139000000
closest = c / 186000*1
farthest = f / 186000*1
average = a / 186000*1

import time

def gettime(sec):
    return time.strftime('%H hours, %M minutes, %S seconds', time.gmtime(sec))


print("The photo will take ",gettime(closest)," to reach home.")
print("The photo will take ",gettime(farthest)," to reach home.")
print("The photo will take ",gettime(average)," to reach home.")

and the result will be:结果将是:

The photo will take  00 hours, 03 minutes, 02 seconds  to reach home.
The photo will take  00 hours, 22 minutes, 18 seconds  to reach home.
The photo will take  00 hours, 12 minutes, 27 seconds  to reach home.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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