[portland] New data, old script: new problem
monk at netjunky.com
Wed Mar 23 02:51:26 CET 2011
On Tue, Mar 22, 2011 at 5:57 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> Me again. Python is telling me that there are 13 items in the row count in
> addition to row where there should be only 12. I don't see the error and
> there is no blank space at the end of the row.
It looks to me like your data headers (location and date) have two
blanks at the beginning of the rows (two leading colons), while the
sampled data has only one blank (one leading colon).
That will cause loc and sampdate to have 14 elements each while row
will only have 13.
You could adjust your subscripting to use row[i-1] and that would
solve your quandary.
Also, it looks like loc and sampdate are blank. Are you
intentionally including them in your output? You may want to skip
processing if i < 2, otherwise your first data column will also be
> 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
> 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.
> -------------- next part --------------
> #!/usr/bin/env python
> import sys,csv
> filename = sys.argv
> infile = open(filename, 'r')
> print "Can't open ", filename,"!"
> 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:
> # 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] != '':
> outdata.writerow([location, sampdate[i], row, float(row[i])])
> row[i] = ''
> outdata.writerow([location, sampdate[i], row])
> Portland mailing list
> Portland at python.org
More information about the Portland