Generating text files (newbie)

2006-12-28 Thread Doran, Harold
Assume I have a tab-delimited text file called foo.txt organized as
follows:

x11 -0.04   
x22 -0.42   
x33 0.3

My goal is to read in this file and use the information therein to
output a new file that is organized as follows:


x11 IRM=3PL IPB= -0.04  
x22 IRM=3PL IPB= -0.42  
x33 IRM=3PL IPB= 0.3

I am a newbie with python and am trying to put the pieces together. Now,
I know if I had a list, I could do something like:

a = [-0.04, -0.42, 0.3]

 for i in a:
print IRM = 3PL, \t, IPB = , i

IRM = 3PL   IPB =  -0.04
IRM = 3PL   IPB =  -0.42
IRM = 3PL   IPB =  0.3

And then I could write this to a file. But I am not sure how to I might
tackle this when I read in a text file. 

So far, my basic skills allow for me to accomplish the following

# read in file
params = open('foo.txt', 'r+')

# Write to the file
params.write('Add in some new test\n')

params.close()

Any pointers are appreciated. I'm using python 2.3 on a windows xp
machine.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Generating text files (newbie)

2006-12-28 Thread Gabriel Genellina

At Thursday 28/12/2006 10:59, Doran, Harold wrote:


Assume I have a tab-delimited text file called foo.txt organized as follows:

x11 -0.04
x22 -0.42
x33 0.3

My goal is to read in this file and use the information therein to 
output a new file that is organized as follows:


x11 IRM=3PL IPB= -0.04
x22 IRM=3PL IPB= -0.42
x33 IRM=3PL IPB= 0.3


Two ways:
- Reading the file manually. I recommend this because it's easy in 
this case and can help you developing the Python skills you want.


params = open(foo.txt,rt)
output = open(bar.txt,wt)
for line in params:
x, y = line.split('\t')
y = float(y) # make sure it's a number
output.write(%s IRM=3PL IPB=%f\n % (x, y))
output.close()
params.close()

- Using the standard csv module:

import csv
params = csv.reader(open(foo.txt, r), delimiter='\t')
output = open(bar.txt,wt)
for row in params:
output.write(%s IRM=3PL IPB=%s\n % (row[0], row[1]))
output.close()
params.close()

--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

-- 
http://mail.python.org/mailman/listinfo/python-list