[英]Python: TimeDelta how to show only minutes and seconds
I have the following code:我有以下代码:
if (len(circles[0, :])) == 5:
start = time.time()
if (len(circles[0, :])) == 1:
end = time.time()
print(str(timedelta(seconds=end-start)))
This is the output:这是输出:
0:01:03.681325
And this is the output i am looking to achieve:这是我希望实现的输出:
1:03
If you rely on just the default string representation of a timedelta
object, you will get that format.如果您仅依赖timedelta
对象的默认字符串表示,您将获得该格式。 Instead, you will have to specify your own formatting:相反,您必须指定自己的格式:
from datetime import timedelta
def convert_delta(dlt: timedelta) -> str:
minutes, seconds = divmod(int(dlt.total_seconds()), 60)
return f"{minutes}:{seconds:02}"
# this is your time from your code
dlt = timedelta(minutes=1, seconds=3, milliseconds=681.325)
print(convert_delta(dlt))
And with this you get this output:有了这个,你会得到这个输出:
1:03
根据 wkl 的回答,您可以使用int(input())
输入分钟、秒,使用 float(input()) float(input())
毫秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.