Optimizing this code

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Optimizing this code

 
0
  #1
May 26th, 2009
First let me say thanks to everyone who has been responding to my posts and providing valuable insight. I have been trying to add rep whenever possible, and appreciate your help.

My assignment was to write a python code which takes data from an infile, then has the user specify a column out of the infile, and then the program writes only the information from that column to an outfile. Thanks to help from you guys, I've also been able to include a portion of the code which will skip over blank lines instead of crashing. I'd like to optimize/change the code with your suggestions. I feel that I've probably included too many operations and am being redundant in places. Here is my code, with comments which try to convey my level of understanding. Please add suggestions and point out where my comments are fallacious.

  1. def columns(infile, outfile):
  2. f = open(infile,'r')
  3. o = open(outfile,'w')
  4.  
  5. col = int(raw_input('Please select a column (starting at 0) from you in file, %s:' % (infile)))
  6.  
  7. temp = [] #Store an empty list
  8.  
  9. for line in f:
  10. if not line.strip(): #If empty line, strip the new line character and remaining space
  11. continue
  12. else:
  13. line = line.split() #Split lines so that they can be operated on via list operations
  14. temp.append(line[col]) #Write the user-specified column into the empty list, "temp
  15.  
  16. o.write('\n'.join(temp))
  17.  
  18.  
  19. print "See %s" % (outfile)
  20.  
  21. f.close()
  22. o.close()
Last edited by shoemoodoshaloo; May 26th, 2009 at 11:23 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 74
Reputation: billymcguffin is an unknown quantity at this point 
Solved Threads: 1
billymcguffin billymcguffin is offline Offline
Junior Poster in Training

Re: Optimizing this code

 
0
  #2
May 26th, 2009
this isnt that big of a deal, but you could add col -= 1 after this:
  1. col = int(raw_input('Please select a column (starting at 0) from you in file, %s:' % (infile)))
this way, you can eliminate the "starting at 0" part and they will enter "1" for the first column.
Last edited by billymcguffin; May 26th, 2009 at 11:28 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 70
Reputation: shoemoodoshaloo is an unknown quantity at this point 
Solved Threads: 2
shoemoodoshaloo shoemoodoshaloo is offline Offline
Junior Poster in Training

Re: Optimizing this code

 
0
  #3
May 26th, 2009
Cool suggestion, added.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 74
Reputation: billymcguffin is an unknown quantity at this point 
Solved Threads: 1
billymcguffin billymcguffin is offline Offline
Junior Poster in Training

Re: Optimizing this code

 
0
  #4
May 26th, 2009
sure
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Optimizing this code

 
0
  #5
May 26th, 2009
Some of your comments may be overkill. Here's how I'd revise your function:

* Added Billy's suggestion from above
* I like comments on separate lines unless they're extremely short
* Tabs are an absolute no-no in my book
* No need to say if something: continue ... when there's no other action in the loop, simply omit the check for something
  1. def columns(infile, outfile):
  2. f = open(infile,'r')
  3. o = open(outfile,'w')
  4.  
  5. col = raw_input('Please select a column from your input file, %s:' % (infile))
  6. col = int(col) - 1
  7.  
  8. temp = []
  9. for line in f:
  10. line = line.strip()
  11. if line:
  12. # Split each line into columns
  13. line = line.split()
  14. # Write contents of user-specified columns to temp
  15. temp.append(line[col])
  16. o.write('\n'.join(temp))
  17.  
  18.  
  19. print "See %s" % (outfile)
  20. f.close()
  21. o.close()
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 127
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: Optimizing this code

 
0
  #6
May 27th, 2009
You unnecessary use the list.

  1.  
  2. def columns(infile, outfile):
  3. f = open(infile,'r')
  4. o = open(outfile,'w')
  5.  
  6. col = raw_input('Please select a column from your input file, %s:' % (infile))
  7. col = int(col) - 1
  8. for line in f:
  9. line = line.strip()
  10. if line:
  11. # Split each line into columns
  12. line = line.split()
  13. # Write contents of user-specified columns to temp
  14. o.write(line[col])
  15. o.write("\n")
  16. f.close()
  17. o.close() # this is the actual flushing
  18. print "See %s" % (outfile)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Optimizing this code

 
0
  #7
May 28th, 2009
If you're really looking to get fancy use some list comprehension:
  1.  
  2. def columns(infile, outfile):
  3. f = open(infile,'r')
  4. o = open(outfile,'w')
  5. #
  6. col = raw_input('Please select a column from your input file, %s:' % (infile))
  7. col = int(col) - 1
  8. o.write('\n'.join([line.strip().split()[col] for line in f if line != '\n']))
  9. #
  10. f.close()
  11. o.close() # this is the actual flushing
  12. print "See %s" % (outfile)
Last edited by jlm699; May 28th, 2009 at 9:57 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC