I'll open with a mea culpa: I've done no python coding in more than a
year, have lost most of my knowledge, and need to regain (and exceed) the
proficiency I once had. My immediate need is to restructure data exported
from spreadsheets as .csv files. I'm trying to use the csv module, but keep
going around in circles trying to index each column in each row so they can
be written to an output file with a different organization.
Here are the first three lines of an exported file (10b20b.txt):
:'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-10B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B':'JCM-20B'
:2004-08-16:2005-06-07:2005-08-31:2006-06-12:2006-09-21:2007-06-21:2007-12-11:2008-06-09:2008-12-04:2009-06-23:2009-10-15::2004-08-16:2005-06-08:2005-08-16:2006-06-02:2006-12-29:2007-06-08:2007-12-11:2008-06-09:2008-11-13:2009-06-30:2009-12-09:
'Depth to
Water':76.75:77.51:82.15:73.17:72.66:84.44:96.41:92.15:97.95::::90.72:90.74:91.65:87.41:86.81:91.72:96.42:97.03:97.08:::
The first two rows have NULL values for the first column (which I'll call
param).
The goal of the exercise is to write an output .csv file suitable for
import to a database table. This means concatenating row 0, column 1 with
row 1, column 1 and row 2, column 0, row 2, column 1, row 3, col 0, row 3,
col 1, etc. In other words, I want the output to look like this:
::'JCM-10B':'2004-08-16':'Depth to water':76.75::
::'JCM-10B':'2004-08-16':<next param>:<next value>::
What I've written so far doesn't do the job, and I'm not seeing the proper
approach to this. Using the csv.reader() method does not allow subscripting
so all I'be been able to print is the first column of each row. Not very
good.
datacon.py:
#!/usr/bin/env python
"""
This file converts the exported wq excel files to have each row contain the
location, date, parameter, and value.
"""
import csv
reader = csv.reader(open("10b20b.txt","rb"), delimiter=":", quotechar="'")
output = csv.writer(open("out.txt","wb"))
try:
for row in reader:
param = row[0]
site = row[1]
print param, site
#output.writerow(row[0])
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (output, reader.line_num, e))
Because I have several dozen such files to convert, this exercise is a
great way for my getting my python back into working memory. I think the
problem is integrating the csv module with manipulating a 2-dimensional
array [row][col] and adding the NULL separators so the output can be
imported to an existing database table.
Pointers are needed; thanks in advance.
Rich
_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland