[Tutor] One more (sorry)
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sat, 9 Feb 2002 11:42:18 -0800 (PST)
On Sat, 9 Feb 2002, kevin parks wrote:
> So if i have a file that looks like this:
>
> 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
>
> and i want to add .5 to all of foo's column 2 to get:
>
> foo 1.5 5.0 10.0
> foo2 1.0 5.0 15.0
> foo 3.0 3.0 9.00
> foo 3.6 5.5 12.00
> foo3 5.0 1.25 7.00
[some text cut]
>
> How would I do that?
[more text cut]
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 = open(filename)
rows = []
for line in f.readlines():
columns = line.split()
rows.append(columns)
return rows
###
It's not perfect (and also untested. *grin*), because it does leave the
"numbers" as strings, so we have to be a little careful when we're about
to do something like "add 0.5 to every column in the foo row."
Hope this helps!