繁体   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