I have an exception thrown for an index error that I do not see. Running
the script through winpdb gives me no more insight into why the error is
there (on line 27).

Traceback (most recent call last):
  File "./wide-to-long.py", line 27, in <module>
    output.write(str(chems[h]))
IndexError: list index out of range

Here's the script:

#!/usr/bin/env python

import sys

"""
First row of input file is list of chemical constituents. Following rows have
site ID, sample date, and a quantity for each constituent.
"""
filename = sys.argv[1]

infile = open(filename, 'r')
output = open("out.txt","w")

# read column headers into a list
chems = infile.readline().split(',')
h = 0 # header list index

# read each data line
d = 2 # data list index

for line in infile.read().split('\n'):
  data = line.split(',')
  output.write(str(data[0]))
  output.write(',')
  output.write(str(data[1]))
  output.write(',')
  output.write(str(chems[h])) # site of index error.
  output.write(',')
  output.write(str(data[d]))
  output.write('\n')
  if h <= len(chems):
    h += 1
  if d <= len(chems):
    d += 1

infile.close()
output.close()

------------------------------

  Sample data:

Ag,Al,CO3,HCO3,AlkTot,As,Ba,Be,Bi,Ca,Cd,Cl,Co,Cr,Cu,DO,Fe,Hg,K,Mg,Mn,Mo,Na,NH4,NO3NO2,oil_grease,Pb,pH,Sb,SC,Se,SO4,Sr,TDS,Tl,V,Zn
D-1,2007-12-12,-0.005,0.106,-1.000,231.000,231.000,0.011,0.000,-0.002,0.000,100.000,0.000,1.430,0.000,-0.006,0.024,4.960,4.110,,0.000,9.560,0.035,0.000,0.970,-0.010,0.293,,0.025,7.800,-0.001,630.000,0.001,65.800,0.000,320.000,-0.001,0.000,11.400
D-1,2008-03-15,-0.005,-0.080,-1.000,228.000,228.000,0.001,0.000,-0.002,0.000,88.400,0.000,1.340,0.000,-0.006,0.014,9.910,0.309,0.000,0.000,9.150,0.047,0.000,0.820,0.224,-0.020,,0.025,7.940,-0.001,633.000,0.001,75.400,0.000,300.000,-0.001,0.000,12.400
D-1,2008-06-26,-0.005,0.116,6.700,118.000,124.000,0.010,0.000,-0.002,0.000,63.400,0.000,1.750,0.000,-0.006,0.020,4.320,2.830,0.000,0.000,9.550,0.020,0.000,0.653,-0.010,-0.050,,0.025,8.650,0.001,386.000,-0.001,68.500,0.000,480.000,-0.001,0.000,5.500

  A clue stick appropriately applied will be very helpful.

TIA,

Rich

_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland

Reply via email to