2011-12-02 4 views
2

나는 현재 다음과 같이 두 번 차이를 계산하려고합니다. out-in은 매우 빠르므로 나는 단지 0.00 인 시간과 분을 표시 할 필요가 없다. 실제로 어떻게 파이썬에서 소수점 자리를 이동합니까?어떻게 파이썬에서 소수점 자리를 바꿀 수 있습니까?

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) 
관련 문제