我是python的新手,我的输出有问题。我正在使用Ipython编写我的代码。我想要做的是同步两个时钟时间。我首先打开一个csv,其中一个来自一个源中的一个源,另一个来自另一个源中的时间。一旦我发现这些值之间的差异,我导入另一个csv,它具有滞后的时间源中的所有值,并添加差异,以便滞后时钟现在是另一个的标准时钟。 问题: 1.当我切换出第一个csv以计算另一个时间差时,例如(第一个csv时间差为5分钟,新的csv时间差为2分钟),输出将增加5分钟而不是2分钟。我附上了代码,你可以看到。 2.我也不知道如何将日期时间对象转换为时间对象以摆脱1900-01-01。我看到了论坛,但我不明白代码,所以如果有一个简单的方法,我会很感激。
我知道我缺少很多基础知识,但我很感激帮助!
import csv
from datetime import time
time_difference= open('TEST.csv')
time_difference_csv=csv.reader(time_difference)#imports csv with the two #clock differences; e.g wall clock and scanner clock one value in each column
firstline = True
for row in time_difference_csv:
if firstline: #skip first line
firstline = False
continue
# parse the line
for row in time_difference_csv:#converts the string values of the clock #into datetimes which allows them to be subtracted and added
w = [[datetime.time.strptime(i[0],'%H:%M:%S')]+ [datetime.time.strptime(i[1],'%H:%M:%S')] for i in time_difference_csv]
for row in w:#gets the difference btwn the two clocks
p= (row[1]-row[0])
for row in w:
l= (row[1]-row[0]) + row[0]
td= open('121Times3.csv')#imports csv with the list of times from one time source; e.g. just the scanner times
td_csv=csv.reader(td)
firstline = True
for row in td_csv:
if firstline: #skip first line
firstline = False
continue
# parse the line
for row in td_csv:
k = [[datetime.time.strptime(i[0],'%H:%M:%S')] for i in td_csv]
for row in k:#adds the difference found earlier to all the times on the csv. Converts all the scanner times to the wall times.
print row[0] + p
输出 1900-01-01 10:37:09 1900-01-01 11:48:54 1900-01-01 11:19:08 1900-01-01 10:37:09 1900-01-01 11:48:54 1900-01-01 11:19:08 1900-01-01 10:37:09 1900-01-01 11:48:54 1900-01-01 11:19:08