“ Test.csv”文件有两列,它们都包含时间。我想将一列的时间减去另一列的时间,然后将其写入新文件“ abc.csv”。我的代码运行良好,但是在“ abc.csv”中,两行用空白行隔开。如何删除? 代码:
import csv
from datetime import datetime
def time_cat(time1,time2):
FMT = '%H:%M:%S'
tdelta = abs(datetime.strptime(time1, FMT) - datetime.strptime(time2, FMT))
return tdelta
name="test.csv"
with open(name, 'r') as csvFileF:
F_reader=csv.reader(csvFileF)
lines = list(F_reader)
time_list=[]
for i in range(len(lines)):
timedif=time_cat(lines[i][2],lines[i][1])
time_list.append(timedif)
outfile = open('abc.csv','w')
out = csv.writer(outfile)
out.writerows(map(lambda x:[x], time_list))
outfile.close()