2016-12-29 189 views
0

所以我寫了一個python代碼,將秒轉換成小時分鐘和秒,但代碼看起來不整齊。什麼是更好的方式來做我的腳本正在做什麼?有沒有更簡單的方法來做到這一點?

from fractions import Fraction as Frac 
from decimal import Decimal as Deci 

def Hrs(mins,hr=[],deci=[],hrs=True): 
    return(Seperator(mins/60)) 

def Seperator(Decimal,num=[],deci=[],nums=True): 
    del num[:],deci[:] 
    for item in str(Decimal): 
    if item == '.': 
    deci.append(item) 
    nums = False 
    continue 
    num.append(item)if nums else deci.append(item) 
    return''.join(num),''.join(deci) 

def Mins(secs): 
    t = Deci(float(Frac(secs,60))) 
    return t 

def Secs(deci): 
    r = Deci(float(deci*60)) 
    return r 

def Clean(decimal,deci=[],p=True): 
    del deci[:] 
    for item in str(decimal): 
    if item == '.': 
    if p:deci.append(item);p=False;continue 
    else: break 
    deci.append(item) 
    return float(''.join(deci)) 

if __name__ == '__main__': 
    Seconds = 6033 
    Hours = Hrs(Mins(Seconds))[0] 
    Minutes = int(Secs(Clean(Hrs(Mins(Seconds))[1]))) 
    Seconds = int(eval(Seperator(Secs(Clean(Hrs(Mins(Seconds))[1])))[1])*60) 
    print '{}:{}:{}'.format(Hours,Minutes,Seconds) 

回答

2

絕對如此。在這裏你去:

def convert(total): 
    secs = total%60 
    total /= 60 
    minutes = total%60 
    total /= 60 
    hours = total%24 
    days = total/24 
    return (days, hours, minutes, secs) 

convert(13246345) 

輸出:(153, 7, 32, 25)

0

這應該做的伎倆

import time 

localtime = time.localtime() 
timeString = time.strftime("%Y%m%d%H%M%S", localtime) 

timeString = time.strftime("%H:%M:%S") 
print timeString 
相關問題