[portland] New data, old script: new problem
Rich Shepard
rshepard at appl-ecosys.com
Wed Mar 23 01:57:12 CET 2011
Me again. Python is telling me that there are 13 items in the row count in
addition to row[0] where there should be only 12. I don't see the error and
there is no blank space at the end of the row.
I've attached the script (datacon.py) that worked before, and here's a
very short data set that throws the error:
Traceback (most recent call last):
File "./datacon.py", line 32, in <module>
if row[i] != '':
IndexError: list index out of range
Data:
::JC-2:JC-3:JC-3:MC-1:MC-2:BC-2:BC-3:ITSN:SC:SC-100:GDSP-10:GD-1
::2008-06-26:2008-05-29:2008-06-26:2008-06-26:2008-06-26:2008-06-26:2008-05-29:2008-06-23:2008-06-26:2008-06-23:2008-06-23:2008-06-26
pH:8.00:8.00:7.50:8.00:7.90:8.00:7.90:8.40:7.50:8.00:8.00:8.10
I've printed stubs and found that the list index is looking for one too
many items, but I don't see where it's getting the incorrect count. Please
point out what's incompatible with the script or data file.
TIA,
Rich
-------------- next part --------------
#!/usr/bin/env python
import sys,csv
filename = sys.argv[1]
try:
infile = open(filename, 'r')
except:
print "Can't open ", filename,"!"
sys.exit(1)
indata = csv.reader(infile, delimiter=':')
loc = indata.next()
sampdate = indata.next()
# check for mis-match in number of columns
assert len(loc) == len(sampdate)
outfile = open('out.csv', 'w')
outdata = csv.writer(outfile, delimiter = ':', lineterminator = '\n')
# The next rows are our parameters.
for row in indata:
# iterate through each column for the particular locations
for i, location in enumerate(loc):
# ignore the first column, which contains the parameter name
if i == 0:
continue
# Write the location, the date for this column, the parameter name
# (which is the first column in this row), and the value.
if row[i] != '':
float(row[i])
outdata.writerow([location, sampdate[i], row[0], float(row[i])])
else:
row[i] = ''
outdata.writerow([location, sampdate[i], row[0]])
infile.close()
outfile.close()
More information about the Portland
mailing list