我有這個函式將秒轉換為 dd:hh:mm:ss (string) - 但是,當輸入列中有一個空實體時,我收到錯誤 PythonException: 'TypeError: unsupported operation type(s) for divmod( ): 'NoneType' 和 'int''。
有沒有可以放在函式內部的修復,下面 -
def to_hms(s):
m, s = divmod(s, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
return '{}:{:0>2}:{:0>2}:{:0>2}'.format(d, h, m, s)
uj5u.com熱心網友回復:
也許這可以幫助:
def to_hms(s):
if s:
m, s = divmod(s, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
return '{}:{:0>2}:{:0>2}:{:0>2}'.format(d, h, m, s)
return '0:00:00:00'
如果沒有找到 s 的值,這個將回傳空值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/364517.html
上一篇:Python精簡if陳述句
