簡體   English   中英

如何在Python中將整數轉換為高分辨率時間? 還是如何防止Python掉零?

[英]How do I convert integers into high-resolution times in Python? Or how do I keep Python from dropping zeros?

當前,我正在使用它來計算兩則消息之間的時間,並列出兩秒以上的時間。

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],(float(in_ts)*10000 - float(out_ts)*10000))

            n = (float(in_ts)*10000 - float(out_ts)*10000)
            if n> 20:
                print float(out_ts),ref_id[1:-1], n

    INFILE = 'C:/Users/klee/Documents/text.txt'
    import csv 

    with open('output_file1.csv', 'w') as f: 
    csv.writer(f).writerows(time_deltas(INFILE)) 

但是,有兩個主要錯誤。 首先,當時間在10之前,即python時,python會降為零。 0900。並且,它降低了零,使時差不准確。

它看起來像:130203.08766什么時候應該是:130203.087660

您正在yield浮點數,因此csv編寫器可以根據需要將這些浮點數轉換為字符串。

如果您希望輸出值是某種格式,請yield該格式的字符串。

也許像這樣?

print "%04.0f" % (900)     # prints 0900

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM