[英]Calculate elapsed time
I have a text file that always looks like this: 我有一个始终如下所示的文本文件:
977-183 BCA-0055 2014-02-19 10:08:13.61
977-001 BCA-0058 2014-02-19 10:29:08.18
977-013 BCA-0064 2014-02-19 10:28:35.73
977-129 BCA-0079 2014-02-19 10:15:45.24
These are generators. 这些是发电机。 My goal is to add an additional column showing the total hours they have been running until now, which is when the I run the script. 我的目标是添加另一列,以显示它们到目前为止运行的总小时数,即我运行脚本的时间。 I am brand new to python and am stuck. 我是python的新手,被卡住了。 I have figured out how to make a datetime object? 我想出了如何制作日期时间对象? for the now time. 现在。 I had hoped to use this in my calculations (timedelta?) but have not been able to create a datetime for the times in the text file to move on. 我曾希望在计算中使用它(timedelta?),但无法为文本文件中的时间创建日期时间。 I have tried some things and read quite a bit but no luck, here is code showing my latest attempt: 我已经尝试了一些东西,并且阅读了很多但没有运气,下面的代码显示了我的最新尝试:
import datetime
import time
i = datetime.datetime.now()
print i
# this is what the raw data looks like
# 977-183 BCA-0055 2014-02-19 10:08:13.61
raw0 = open("genParse.txt", "r") # text file containing date and time
fin0 = open("elapsed_time.txt", "w") # will contain elapsed times
for line in raw0:
if '977-' in line:
line = line.split()
date = line[2]
time = line[3]
# create datetime object for use in elapsed time calculations
#tnew = datetime.datetime(date[0:4], date[5:7], date[8:10])
print date[0:4], date[5:7], date[8:10]
Use datetime.datetime.strptime()
to parse your strings to datetime.datetime()
objects: 使用datetime.datetime.strptime()
将字符串解析为datetime.datetime()
对象:
dt = datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M:%S.%f')
Now you can calculate the time elapsed: 现在,您可以计算经过的时间:
elapsed = now - dt
which is a timedelta
object. 这是一个timedelta
对象。
Demo: 演示:
>>> import datetime
>>> now = datetime.datetime.now()
>>> date = '2014-02-19'
>>> time = '10:08:13.61'
>>> dt = datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M:%S.%f')
>>> dt
datetime.datetime(2014, 2, 19, 10, 8, 13, 610000)
>>> now - dt
datetime.timedelta(6, 30419, 490913)
The resulting timedelta
object here has a value of 6 days, 30419 seconds and 490913 microseconds. 此处得到的timedelta
对象的值为6天,30419秒和490913微秒。 How you format that to a string is up to you. 如何将其格式化为字符串取决于您。
If you want to get the difference between the entry and when the script is executed: 如果要获得条目和执行脚本之间的区别:
from datetime import datetime
raw0 = open("genParse.txt", "r") # text file containing date and time
fin0 = open("elapsed_time.txt", "w") # will contain elapsed times
for line in raw0:
if '977-' in line:
line = line.split()
dts = '%s %s' % (line[2], line[3])
now = datatime.now()
fmt = '%Y-%m-%d %H:%M:%S.%f'
time_diff = datetime.strptime(now, FMT) - datetime.strptime(dts, FMT)
You can obviously do whatever you want with time_diff
, which is a timedelta
object. 显然,您可以使用time_diff
(它是一个timedelta
对象)来做任何您想做的timedelta
。 Print, to file, etc. This is just expanding on the code you have already written. 打印,保存到文件等。这只是在您已经编写的代码上扩展。 Like always with Python, there are many ways to accomplish the same goal. 与Python一样,有很多方法可以实现相同的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.