将字典列表中的几秒钟转换为小时,python
我正在尝试使用Python的DateTime软件包将词典列表中的秒数转换为小时。例如,而不是“持续时间”:745200(秒)我想要“持续时间”:207:00:00(小时:分钟:秒)。有人可以帮助我如何实现这一目标吗? 谢谢你!
id_duration =
[{'Id': 44, 'Duration': 745200},
{'Id': 45, 'Duration': 259200},
{'Id': 58, 'Duration': 21600},
{'Id': 61, 'Duration': 597600}]
I'm trying to convert the seconds in my list of dictionaries into hours using the DateTime package from python. Eg.g instead of 'Duration': 745200 (seconds) I want 'Duration': 207:00:00 (hours:minutes:seconds). Could someone help me with how to achieve this?
Thank you!
id_duration =
[{'Id': 44, 'Duration': 745200},
{'Id': 45, 'Duration': 259200},
{'Id': 58, 'Duration': 21600},
{'Id': 61, 'Duration': 597600}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,首先要导入软件包
,然后让我们为此定义一个函数,使用
dateTime.timedelta
现在让我们使用您给出的示例进行测试
First of all start by importing the package
Then let's define a function for that, using
datetime.timedelta
Now let's test using the example you give
如果您只需要几个小时,则可以在没有DateTime软件包的情况下尝试NAIVE方法:
If you need just hours, you can try naive approach without DateTime package: