Python将时长转换为MM:SS格式
# -*- coding:utf-8 -*-
#Auth:Jeson?? ? ? ? ?? ?
? ? ? ? ? ? ? ? ? ? ?? ?
def?fmt_duration(seconds):
? ??u""" 將視頻時長轉換成mm:ss的形式 """? ? ? ? ? ? ? ? ?? ?
? ??# 容錯,防止傳空字符串
? ??if?not?seconds:???
? ? ? ? seconds =?0???
? ? # 判斷傳入的seconds類型,和格式。 ? ? ? ? ? ? ? ? ? ?? ?
? ??if?isinstance(seconds,?basestring)?and?': 'in?seconds:
? ? ? ??return?seconds
? ??try:? ?
? ? #格式化字符串 ? ? ? ??? ?
? ? ? ? seconds =?float('{0}'.format(seconds))
? ??except?Exceptionas?e:
? ? ? ? seconds =?0???
? ? ? ??pass
? ? #利用divmod計算整除和取余。<、>分別是左對齊、右對齊?。0>2 表示:至少保留2位,如果不足用0補充。 ? ? ??? ?
? ??return?'{0:0>2}:{1:0>2}'.format(*divmod(int(seconds),?60))
? ? ? ? ? ? ? ? ? ? ?? ?
if?__name__ ==?"__main__":
? ??print?"Begin....."
? ??print?fmt_duration("23423434")
總結
以上是生活随笔為你收集整理的Python将时长转换为MM:SS格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 菜鸟进阶Linux高手之路——第三天
- 下一篇: Twitch 宣布 2016 年第二季度