2011-12-02 142 views
2

我目前使用以下來計算兩次差異。輸入速度非常快,因此我不需要顯示小時和分鐘,只是0.00。我如何實際移動Python中的小數位?如何在Python中移動小數位?

def time_deltas(infile): 
    entries = (line.split() for line in open(INFILE, "r")) 
    ts = {} 
    for e in entries: 
     if " ".join(e[2:5]) == "OuchMsg out: [O]": 
      ts[e[8]] = e[0]  
     elif " ".join(e[2:5]) == "OuchMsg in: [A]":  
      in_ts, ref_id = e[0], e[7] 
      out_ts = ts.pop(ref_id, None) 
      yield (float(out_ts),ref_id[1:-1], "%.10f"%(float(in_ts) - float(out_ts))) 

INFILE = 'C:/Users/kdalton/Documents/Minifile.txt' 
print list(time_deltas(INFILE)) 

回答

12

你在數學

a = 0.01; 
a *= 10; // shifts decimal place right 
a /= 10.; // shifts decimal place left 
+3

手錶整數除法一樣。如果'a'是一個整數,這可能會讓人撓頭。也許把最後一行改爲'a/= 10.'。 –

+0

良好的通話。謝謝:) –

+0

謝謝!如此真實......我想我會讓它變得比本來應該更復雜:D – eunhealee

1

做或使用datetime模塊

>>> import datetime 
>>> a = datetime.datetime.strptime("30 Nov 11 0.00.00", "%d %b %y %H.%M.%S") 
>>> b = datetime.datetime.strptime("2 Dec 11 0.00.00", "%d %b %y %H.%M.%S") 
>>> a - b 
datetime.timedelta(-2)