Generating text files (newbie)
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Dec 28 18:59:48 EST 2006
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
More information about the Python-list
mailing list