[Tutor] rows and columns

Charlie Clark charlie@begeistert.org
Sun, 10 Feb 2002 12:35:13 -0500

>If we read in the file and somehow process it to get something like:
>[ [foo 1.0 5.0 10.0]
>  [foo2 1.0  5.0  15.0]
>  [foo  2.5  3.0  9.00]
>  [foo  3.1  5.5  12.00]
>  [foo3 5.0  1.25 7.00] ]
>That is, a list of columns, then the problem should become a little
>easier.  Here's a function that might do this:
>def readRowsAndColumns(filename):
>    f =3D open(filename)
>    rows =3D []
>    for line in f.readlines():
>        columns =3D line.split()
>        rows.append(columns)
>    return rows
My guess is you're wanting to write the changed lines back out into a 
file. If you don't need to that you might think about storing the data 
in a more flexible form such as a list or dictionary or a class which 
you can save to disk using pickle.

def readRowsAndColumns(filename):
   f =3D open(filename)
   out =3D open("output=5Ffile.txt", "w")
   rows =3D []
   for line in f.readlines():
        columns =3D line.split()
	"""convert the second value in each row to a number, add 0.5 to it 
and turn back to a string"""
        columns[1] =3D str(float(columns[1] + 0.5))
        out.write(" ".join(columns) + "\n")
   print "finished"

Putting the change function inside like this is not usually the way to 
do things but fine for one of situations.