[Tutor] critique my script: add columns in a file

Christopher Spears cspears2002 at yahoo.com
Wed Feb 14 06:46:45 CET 2007


I created a file called table.txt.  Here is the file's
contents:

1       5       10      2       1.0
2       10      20      4       2.0     3
3       15      30      8       3       2       1
4       20      40      16      4.0

I modified a script I found in "Programming Python"
and created script called summer_v03.py that added the
columns together succesfully and outputted the results
in a list:

io at io-station-1 ./text_proc 158> ./summer_v03.py
table.txt
[10.0, 50.0, 100.0, 30.0, 10.0, 5.0, 1.0]

Here is the code:

#!/usr/bin/python
import string

def find_longest_line(fileName):
	longest_col = []
	for lines_in_file in open(fileName, 'r').readlines():
		cols_in_file = string.split(lines_in_file)
		#print cols_in_file
		numCols = len(cols_in_file)
		if numCols > len(longest_col):
			longest_col = cols_in_file
	return len(longest_col)
		

def summer(fileName):
	length_longest_col = find_longest_line(fileName)
	sums = [0] * length_longest_col
	for lines_in_file in open(fileName, 'r').readlines():
		cols = string.split(lines_in_file)
		for i in range(len(cols)):
			sums[i] = sums[i] + float(cols[i])
	return sums
			
if __name__ == '__main__':
	import sys
	print summer(sys.argv[1])

What do you think?


"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton


More information about the Tutor mailing list