[Tutor] rows and columns

Gregor Lingl glingl@aon.at
Sun, 10 Feb 2002 20:06:20 +0100


Nice, except that I think Kevin additionally wanted this:

"""if row begins with 'foo' then
convert the second value in each row to a number, add 0.5 to it 
and turn back to a string"""
        if columns[0]=='foo':
            columns[1] = str(float(columns[1] + 0.5))


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 = open(filename)
   out = open("output_file.txt", "w")
   rows = []
   for line in f.readlines():
        columns = 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] = str(float(columns[1] + 0.5))
        out.write(" ".join(columns) + "\n")
   out.close()
   print "finished"

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

Charlie 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor