[Tutor] better looping construct for replacing elements in file?

2012-02-02 Thread Brett Longworth

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:

Y002BET291810/18/06 15:32:52160.001741.000
164081.306E-121.213E-10402.6405.9-42.73.2
1.2242-0.02201.822-12.66A499215631
86523data3.7E-6
Y002BET291810/18/06 15:35:46150.001621.000
156541.313E-121.226E-10407.6410.3-43.92.0
1.2180-0.02431.894-13.03A499215631
865233.7E-6
0003BET714710/18/06 15:55:33170.001861.000
34422.903E-132.693E-11357.7359.3-46.12.5
1.20000.02761.734-12.86A499315631
865243.3E-6
0003BET714710/18/06 15:58:49    170.00    1851.000
32322.772E-132.598E-11351.8353.4-46.13.5
1.20000.01491.761-12.66A499315631
865243.2E-6
0003BET714710/18/06 16:02:06170.001851.000
33992.955E-132.753E-11346.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


Re: [Tutor] better looping construct for replacing elements in file?

2012-02-02 Thread Brett Longworth

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  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:

Y002BET291810/18/06 15:32:52160.001741.00016408
  1.306E-121.213E-10402.6405.9-42.73.21.2242
  -0.02201.822-12.66A49921563186523data
  3.7E-6
Y002BET291810/18/06 15:35:46150.001621.00015654
  1.313E-121.226E-10407.6410.3-43.92.01.2180
  -0.02431.894-13.03A499215631865233.7E-6
0003BET714710/18/06 15:55:33170.001861.0003442
  2.903E-132.693E-11357.7359.3-46.12.51.2000
  0.02761.734-12.86A499315631865243.3E-6
0003BET7147    10/18/06 15:58:49170.001851.0003232
  2.772E-132.598E-11351.8353.4-46.13.51.2000
  0.01491.761-12.66A499315631865243.2E-6
0003BET714710/18/06 16:02:06170.001851.0003399
  2.955E-132.753E-11346.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