[portland] List Indexing Confusion
rshepard at appl-ecosys.com
Thu Aug 11 23:31:34 CEST 2011
Once again it's been too long since I've written a script; every time I
mean to finish a model I've been writing a more critical business need has
pushed the coding back.
I'm now faced with translating a couple of dozen spreadsheets (saved as
.csv files) into the proper format for insertion into a database table. My
brain refuses to get the row indexing correct.
Here is the first few rows of a typical file:
Arsenic:Zinc:Nitrate Nitrogen:pH:Chloride:Sulfate:Total Dissolved Solids
I want an output file that looks like
The mal-functioning script is:
filename = sys.argv
infile = open(filename, 'r')
print "Can't open ", filename,"!"
indata = csv.reader(infile, delimiter=':')
loc = indata.next() # only one field on first line
parmlist = indata.next() # the list of chemicals
outfile = open('out.csv', 'w')
outdata = csv.writer(outfile, delimiter = '|', lineterminator = '\n')
i = 0
j = 0
for row in indata:
outdata.writerow([loc, row[i][j], parmlist[i], row[i][j+1]])
i += 1
j += 1
List indexing is not that difficult so I am embarrassed to admit that I
don't see what I'm doing incorrectly. A clue would be very helpful.
More information about the Portland