[portland] Not Seeing Script Error(s)

Rich Shepard rshepard at appl-ecosys.com
Tue Nov 22 18:32:36 CET 2011


   After several years I need to get back up to speed writing python
scripts. The immediate need is re-formatting a text file (with a .csv
extention) exported from Excel to the format needed by a postgres table (and
an R data frame). The script seems to work (python throws no overt errors),
but it's showing a python object in the output and that should not be there.

   The script:

#!/usr/bin/env python

import sys,csv

"""
First row of input file is list of chemical parameters. Following rows have
sample number, site ID, sample date, and a quantity for each constituent.
"""

filename = sys.argv[1]
try:
   infile = open(filename, 'r')
except:
   print "Can't open ", filename,"!"
   sys.exit(1)
indata = csv.reader(infile, delimiter='|')

# the list of chemicals
parmlist = indata.next()

outfile = open('out.txt', 'w')
outdata = csv.writer(outfile, delimiter = '|', lineterminator = '\n')

# The next rows are sample numbers, site ids, dates, and values.

for row in indata:
   for parm, rowval in zip(parmlist,row[0:]):
     outdata.writerow([outdata, row[0:3], parm, rowval])

infile.close()
outfile.close()

    Some sample data:

|||Ag|Al|Alk_CO3|Alk_HCO3|Alk_tot|As|Ba|Be|Bi|Ca|Cd|Cl|Co|Cr|Cu|DO|Fe|Hg|K|Mg|Mn|Mo|Na|NH4-N|Na|NO3-NO2-N|oil_grease|Pb|pH|Sb|SC|Se|SO4|Sr|TDS|Tl|V|Zn
1|SW-1|1990-10-10|0.000|0.000|0.000|213.000|213.000|0.000|0.000|0.000|0.000|66.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|0.000|14.000|0.000|0.000|1.000|0.080|0.000|0.020|NA|0.025|8.640|0.000|596.000|0.000|43.000|0.000|270.000|0.000|0.000|0.000
2|SW-2|1990-10-10|0.000|0.000|0.000|178.000|178.000|0.000|0.000|0.000|0.000|55.000|0.000|1.000|0.000|0.000|0.000|NA|0.000|0.000|1.640|9.000|0.000|0.000|9.000|0.080|0.000|0.020|NA|0.025|8.690|0.000|516.000|0.000|19.000|0.000|220.000|0.000|0.000|0.000
3|SW-3|1990-10-10|0.000|0.000|0.000|183.000|183.000|0.000|0.000|0.000|0.000|69.000|0.000|1.000|0.000|0.000|0.000|NA|0.000|0.000|0.000|6.000|0.000|0.000|7.000|0.080|0.000|0.020|NA|0.025|8.630|0.000|559.000|0.000|29.000|0.000|224.000|0.000|0.000|0.000
4|SW-1|1990-11-29|0.000|0.000|0.000|217.000|217.000|0.000|0.000|0.000|0.000|68.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|14.000|0.000|0.000|10.000|0.050|0.000|0.020|NA|0.025|8.520|0.000|6.630|0.000|24.000|0.000|256.000|0.000|0.000|0.000
5|SW-2|1990-11-29|0.000|0.000|0.000|170.000|170.000|0.000|0.000|0.000|0.000|57.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|1.000|10.000|0.000|0.000|8.000|0.050|0.000|0.020|NA|0.025|8.710|0.000|6.000|0.000|17.000|0.000|206.000|0.000|0.000|0.000
6|SW-3|1990-11-29|0.000|0.000|0.000|180.000|180.000|0.000|0.000|0.000|0.000|74.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|6.000|0.000|0.000|4.000|0.050|0.000|0.020|NA|0.025|8.500|0.000|6.760|0.000|28.000|0.000|226.000|0.000|0.000|0.000
7|SW-1|1991-03-28|0.000|0.000|4.000|172.000|176.000|0.000|0.000|0.000|0.000|50.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|9.000|0.000|0.000|8.000|0.140|0.000|0.020|NA|0.025|8.100|0.000|5.720|0.000|7.000|0.000|182.000|0.000|0.000|0.000
8|SW-2|1991-03-28|0.000|0.000|8.000|204.000|212.000|0.000|0.000|0.000|0.000|59.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|14.000|0.000|0.000|10.000|0.090|0.000|0.020|NA|0.025|8.400|0.000|6.520|0.000|12.000|0.000|220.000|0.000|0.000|0.000
9|SW-3|1991-03-28|0.000|0.000|0.000|194.000|194.000|0.000|0.000|0.000|0.000|69.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|6.000|0.000|0.000|6.000|0.090|0.000|0.020|NA|0.025|8.100|0.000|6.920|0.000|25.000|0.000|224.000|0.000|0.000|0.000
10|SW-1|1991-05-27|0.000|0.000|3.000|153.000|156.000|0.000|0.000|0.000|0.000|48.000|0.000|4.000|0.000|0.000|0.000|NA|0.000|0.000|1.000|8.000|0.000|0.000|9.000|0.050|0.000|0.020|NA|0.025|8.350|0.000|5.100|0.000|21.000|0.000|230.000|0.000|0.000|0.000
11|SW-2|1991-05-27|0.000|0.000|10.000|195.000|205.000|0.000|0.000|0.000|0.000|62.000|0.000|4.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|14.000|0.000|0.000|11.000|0.050|0.000|0.020|NA|0.025|8.360|0.000|5.720|0.000|25.000|0.000|266.000|0.000|0.000|0.000
12|SW-3|1991-05-27|0.000|0.000|10.000|165.000|175.000|0.000|0.000|0.000|0.000|69.000|0.000|1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|5.000|0.000|0.000|4.000|0.050|0.000|0.020|NA|0.025|8.360|0.000|5.720|0.000|29.000|0.000|224.000|0.000|0.000|0.000
13|SW-4|1991-05-27|0.000|0.000|0.000|123.000|123.000|0.000|0.000|0.000|0.000|62.000|0.000|1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|7.000|0.000|0.000|5.000|0.050|0.000|0.020|NA|0.025|8.100|0.000|5.500|0.000|82.000|0.000|222.000|0.000|0.000|0.000
14|SW-5|1991-05-27|0.000|0.000|7.000|182.000|189.000|0.000|0.000|0.000|0.000|73.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|0.000|6.000|0.000|0.000|6.000|0.050|0.000|0.090|NA|0.025|8.310|0.000|6.290|0.000|16.000|0.000|248.000|0.000|0.000|0.000
15|SW-1|1991-06-09|0.000|0.000|8.000|152.000|160.000|0.000|0.000|0.000|0.000|48.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|1.000|8.000|0.000|0.000|7.000|0.050|0.000|0.030|NA|0.025|8.330|0.000|4.260|0.000|16.000|0.000|202.000|0.000|0.000|0.000
16|SW-2|1991-06-09|0.000|0.000|0.000|216.000|216.000|0.000|0.000|0.000|0.000|61.000|0.000|1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|13.000|0.000|0.000|10.000|0.050|0.000|0.070|NA|0.025|8.350|0.000|6.160|0.000|-2.000|0.000|280.000|0.000|0.000|0.000
17|SW-3|1991-06-09|0.000|0.000|40.000|128.000|168.000|0.000|0.000|0.000|0.000|65.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|5.000|0.000|0.000|4.000|0.050|0.000|0.040|NA|0.025|8.390|0.000|4.700|0.000|25.000|0.000|244.000|0.000|0.000|0.000
18|SW-4|1991-06-09|0.000|0.000|0.000|112.000|112.000|0.000|0.000|0.000|0.000|57.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|6.000|0.000|0.000|5.000|0.050|0.000|0.040|NA|0.025|8.120|0.000|4.700|0.000|78.000|0.000|288.000|0.000|0.000|0.000
19|SW-5|1991-06-09|0.000|0.000|0.000|182.000|182.000|0.000|0.000|0.000|0.000|70.000|0.000|-1.000|0.000|0.000|0.000|NA|0.000|0.000|2.000|6.000|0.000|0.000|5.000|0.050|0.000|0.090|NA|0.025|8.320|0.000|5.800|0.000|33.000|0.000|274.000|0.000|0.000|0.000
20|SW-1|1991-07-23|0.000|0.000|0.000|214.000|214.000|0.000|0.000|0.000|0.000|66.000|0.000|2.000|0.000|0.000|0.000|NA|0.000|0.000|4.000|14.000|0.000|0.000|14.000|0.110|0.000|0.040|NA|0.025|8.280|0.000|0.990|0.000|-2.000|0.000|258.000|0.000|0.000|0.000

   And the output I see in out.csv:

<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|Ag|1
<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|Al|SW-1
<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|Alk_CO3|1990-10-10
<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|Alk_HCO3|0.000
<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|Alk_tot|0.000
<_csv.writer object at 0x80a141c>|['1', 'SW-1', '1990-10-10']|As|0.000

Note that the first three lines repeat the sample number, site id, and date
instead of the value associated with the constituents. I don't see my
error(s) and would greatly appreciate a clue stick on what I've done
incorrectly.

TIA,

Rich


More information about the Portland mailing list