Hi Joel,
Thanks for the reply. The little voice in my head was yelling, "Easier
with SQL!" the entire time, but I'm trying to learn Python.
-Brett
On 2/2/2012 4:16 PM, Joel Goldstick wrote:
On Thu, Feb 2, 2012 at 3:50 PM, Brett Longworth<blongwo...@whoi.edu> wrote:
Hello,
Today I wrote a quick script to replace elements in multiple lines of a file
with corresponding elements from a single line in another file, linking the
two via an index element. The code iterates through the entire source file
to find the matching index for each line of the destination file. This
works, but it's clearly a terrible way to solve the problem. Can someone
point me to a more efficient, pythonic solution?
thanks,
-Brett
Code:
wheel = "A794"
wheelfile = "A794.txt"
statusfile = "CURRENT.ams"
sfile = csv.reader(open(statusfile), delimiter='\t')
statusWriter = csv.writer(open('statustest.txt', 'wb'), delimiter='\t',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for sline in sfile:
#print sline
wfile = csv.reader(open(wheelfile))
for line in wfile:
#print line[0]
#print sline[18]
if line[0] == sline[18]:
sline[0] = line [1]
sline[1] = "OSG"+str(line[4])
sline[17] = wheel
sline[21] = line[9]
statusWriter.writerow(sline)
Excerpt of wheelfile:
"2","X496","02/01/12","OSG","106788","85411","GS-13365","Outside Primary
Standard |> Modern (1950)","2.43","149177"
"3","C655","02/01/12","OSG","106534","83028","HY-19231","Outside Blank |>
30,000","3.63","149178"
Excerpt of statusfile:
Y002 BET2918 10/18/06 15:32:52 160.00 174 1.000 16408
1.306E-12 1.213E-10 402.6 405.9 -42.7 3.2 1.2242
-0.0220 1.822 -12.66 A499 2 1 5631 86523 data
3.7E-6
Y002 BET2918 10/18/06 15:35:46 150.00 162 1.000 15654
1.313E-12 1.226E-10 407.6 410.3 -43.9 2.0 1.2180
-0.0243 1.894 -13.03 A499 2 1 5631 86523 3.7E-6
0003 BET7147 10/18/06 15:55:33 170.00 186 1.000 3442
2.903E-13 2.693E-11 357.7 359.3 -46.1 2.5 1.2000
0.0276 1.734 -12.86 A499 3 1 5631 86524 3.3E-6
0003 BET7147 10/18/06 15:58:49 170.00 185 1.000 3232
2.772E-13 2.598E-11 351.8 353.4 -46.1 3.5 1.2000
0.0149 1.761 -12.66 A499 3 1 5631 86524 3.2E-6
0003 BET7147 10/18/06 16:02:06 170.00 185 1.000 3399
2.955E-13 2.753E-11 346.9
--
Brett Longworth
Research Associate
Woods Hole Oceanographic Institution
ph: 508.289.3559
fax: 508.457.2183
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
If you are a little savvy with sql you could write each csv to tables,
Since you have csv file input why not write each to a db table. Then
you could join on line[0] == sline[18] and update your 4 fields.
Then dump table as csv
--
Brett Longworth
Research Associate
Woods Hole Oceanographic Institution
ph: 508.289.3559
fax: 508.457.2183
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor