在Python中,你可以使用`divmod()`函数或者`datetime`模块来设置时分秒。以下是两种方法的示例代码:
方法一:使用`divmod()`函数
```python
def sec_to_time(sec):
m, s = divmod(sec, 60)
h, m = divmod(m, 60)
return '%02d:%02d:%02d' % (h, m, s)
示例:将秒数转换为时分秒
seconds = 5555
formatted_time = sec_to_time(seconds)
print(formatted_time) 输出:01:32:35
方法二:使用`datetime`模块
```python
from datetime import datetime, timedelta
def sec_to_time(sec):
d = datetime.datetime(1, 1, 1) + timedelta(seconds=sec)
return '%02d:%02d:%02d' % (d.hour, d.minute, d.second)
示例:将秒数转换为时分秒
seconds = 5555
formatted_time = sec_to_time(seconds)
print(formatted_time) 输出:01:32:35
以上两种方法都可以将秒数转换为时分秒的格式。你可以根据你的需要选择使用