[portland] New data, old script: new problem

jonathan karon monk at netjunky.com
Wed Mar 23 02:51:26 CET 2011


Rich,

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[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.

>::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

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[1] and sampdate[1] 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
blank.

~jonathan


>
>  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.
>
> 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()
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
>


More information about the Portland mailing list