Here is one well commented example:
''' data4_expand.py
data4.csv looks like that:
date,time,v1,v2
03/13/2013,11.23.10,12.3,3.4
03/13/2013,11.23.13,12.7,5.1
03/13/2013,11.23.16,15.1,8.3
03/13/2013,11.23.19,17.8,9.8
03/13/2013,11.23.22,19.6,11.4
03/13/2013,11.23.25,21.3,14.6
03/13/2013,11.23.28,24.2,18.3
'''
# read the csv file and process
with open("data4.csv") as fin:
data_list = []
for ix, line in enumerate(fin):
# strip off trailing new line char
line = line.rstrip()
# split at the separator char
tlist = line.split(',')
date, time, v1, v2 = tlist
# treat title line differently
if ix > 0:
hr, mn, sc = time.split('.')
seconds = float(hr)*3600 + float(mn)*60 + float(sc)
data_list.append([date, time, v1, v2, str(seconds)])
else:
# title line
tlist.append('seconds')
data_list.append(tlist)
# write out the expanded csv file
with open("data5.csv", "w") as fout:
# format data string
data_str = ""
for line in data_list:
print ",".join(line) # test
# use appropriate separator char
data_str += ",".join(line) + '\n'
fout.write(data_str)
print('-'*40)
print("Data file {} has been written".format("data5.csv"))
''' test result >>>
date,time,v1,v2,seconds
03/13/2013,11.23.10,12.3,3.4,40990.0
03/13/2013,11.23.13,12.7,5.1,40993.0
03/13/2013,11.23.16,15.1,8.3,40996.0
03/13/2013,11.23.19,17.8,9.8,40999.0
03/13/2013,11.23.22,19.6,11.4,41002.0
03/13/2013,11.23.25,21.3,14.6,41005.0
03/13/2013,11.23.28,24.2,18.3,41008.0
----------------------------------------
Data file data5.csv has been written
'''