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

Kent Johnson kent37 at tds.net
Wed Feb 14 13:53:54 CET 2007


Christopher Spears wrote:
> 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:

It is very wordy. There is no need to read and split the file twice. I 
would just make a list of the (split) lines in the file and keep it 
around. The loops in find_longest_line can easily be replaced with list 
comprehensions.

Some of your names are a little off. lines_in_file is actually a single 
line; cols_in_file is the cols of a single line.

Reading the file into a list of split columns and finding the length of 
the longest line is as  simple as
data = [ line.split() for line in open(fileName) ]
numCols = max(len(cols) for cols in data)

Then your summing loop pretty much stands, though you might want to use 
enumerate() instead of range(len(cols)):
sums = [0] * numCols
for cols in data:
   for i, col in enumerate(cols):
     sums[i] += col

Kent
> 
> #!/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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list