[portland] List Index Out Of Range
Jonathan Karon
monk at netjunky.com
Wed Jul 18 20:02:58 CEST 2012
Whoops. That would output all of the samples from a line of input on a single line in the output, whereas what I think you want is one line per column yes?
This version gives you one line per column:
<snip>
for line in infile.read().split('\n'):
data = line.split(',') # returns a list of strings, no need to cast the elements using str()
sensor = data[0]
timestamp = data[1]
samples = data[2:] # samples is a list of just the values from this row
# note: data file format assumes len(data) == len(chems) + 2, thus len(samples) == len(chems)
for i in range(len(chems)): # loop over the chems header values
output.write(','.join( [ sensor, timestamp, chems[i], samples[i] ] ))
output.write('\n') # line feed
<snip>
On Jul 18, 2012, at 10:55 AM, Jonathan Karon wrote:
> Hi Rich,
>
> It looks to me like what you want to be doing is looping over elements 2 through N in each row of data, i.e. implementing a 2-dimensional loop
>
> maybe this is what you want?
>
> #!/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(',')
>
> d = 2 # offset of first data element in each row
>
>
> for line in infile.read().split('\n'):
> data = line.split(',') # returns a list of strings, no need to cast the elements using str()
> sensor = data[0]
> timestamp = data[1]
> samples = data[2:] # samples is a list of just the values from this row
>
> # note: data file format assumes len(data) == len(chems) + 2, thus len(samples) == len(chems)
>
> line_out = [sensor, timestamp, ] # create a list for generating our output
>
> for i in range(len(chems)): # loop over the chems header values
> line_out.append(chems[i]) # append chem name
> line_out.append(samples[i]) # append sample value
>
> output.write(','.join(line_out)) # create a string by concatenating the output values with comma separator
> output.write('\n') # line feed
>
> infile.close()
> output.close()
>
> On Jul 18, 2012, at 10:31 AM, Rich Shepard wrote:
>
>> 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
>> Portland at python.org
>> http://mail.python.org/mailman/listinfo/portland
>
~jonathan
----
Jonathan Karon
Director, NetJunky Research
Software Architecture & Forward-Looking Mobile Applications
e. monk at netjunky.com p. 503-233-8128 f. 503-334-1258 tw. @fightingmonk
More information about the Portland
mailing list