I think the problem is with how you're referencing the row from indata. I'm not quite sure what you're trying to do with i and j. I'd get rid of i and j and replace the for loop with the following:<div><br>
</div><div>for row in indata:</div><div> for parm, rowval in zip(parmlist,row[1:]): </div><div> outdata.writerow([loc, row[0], parm, rowval])<br><div><br></div><div>(I didn't try the code out, so there may be a typo or some other error.)</div>
<div><br></div><div>Ryan</div><div><br></div><div><br></div><div><br><div class="gmail_quote">On Thu, Aug 11, 2011 at 2:31 PM, Rich Shepard <span dir="ltr"><<a href="mailto:rshepard@appl-ecosys.com">rshepard@appl-ecosys.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> Once again it's been too long since I've written a script; every time I<br>
mean to finish a model I've been writing a more critical business need has<br>
pushed the coding back.<br>
<br>
I'm now faced with translating a couple of dozen spreadsheets (saved as<br>
.csv files) into the proper format for insertion into a database table. My<br>
brain refuses to get the row indexing correct.<br>
<br>
Here is the first few rows of a typical file:<br>
<br>
CVS<br>
Arsenic:Zinc:Nitrate Nitrogen:pH:Chloride:Sulfate:<u></u>Total Dissolved Solids<br>
1993-11-22:0.008:0.014:0.021:<u></u>7.560:2.060:39.3:293.0<br>
<br>
I want an output file that looks like<br>
<br>
CVS|1993-11-22|Arsenic|0.008<br>
CVS|1993-11-22|Zinc|0.014<br>
etc.<br>
<br>
The mal-functioning script is:<br>
-------------------------<br>
#!/usr/bin/env python<br>
<br>
import sys,csv<br>
<br>
filename = sys.argv[1]<br>
try:<br>
infile = open(filename, 'r')<br>
except:<br>
print "Can't open ", filename,"!"<br>
sys.exit(1)<br>
indata = csv.reader(infile, delimiter=':')<br>
<br>
loc = indata.next() # only one field on first line<br>
parmlist = indata.next() # the list of chemicals<br>
<br>
outfile = open('out.csv', 'w')<br>
outdata = csv.writer(outfile, delimiter = '|', lineterminator = '\n')<br>
<br>
i = 0<br>
j = 0<br>
<br>
for row in indata:<br>
outdata.writerow([loc, row[i][j], parmlist[i], row[i][j+1]])<br>
<br>
i += 1<br>
j += 1<br>
<br>
infile.close()<br>
outfile.close()<br>
--------------------------<br>
<br>
List indexing is not that difficult so I am embarrassed to admit that I<br>
don't see what I'm doing incorrectly. A clue would be very helpful.<br>
<br>
Rich<br>
______________________________<u></u>_________________<br>
Portland mailing list<br>
<a href="mailto:Portland@python.org" target="_blank">Portland@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/portland" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/portland</a><br>
</blockquote></div><br></div></div>