943,918 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 396
  • Python RSS
May 26th, 2009
0

Optimizing this code

Expand Post »
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.

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 16
Solved Threads: 4
Junior Poster
shoemoodoshaloo is offline Offline
135 posts
since May 2009
May 26th, 2009
0

Re: Optimizing this code

this isnt that big of a deal, but you could add col -= 1 after this:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
billymcguffin is offline Offline
74 posts
since May 2009
May 26th, 2009
0

Re: Optimizing this code

Cool suggestion, added.
Reputation Points: 16
Solved Threads: 4
Junior Poster
shoemoodoshaloo is offline Offline
135 posts
since May 2009
May 26th, 2009
0

Re: Optimizing this code

sure
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
billymcguffin is offline Offline
74 posts
since May 2009
May 26th, 2009
0

Re: Optimizing this code

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
python Syntax (Toggle Plain Text)
  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()
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 27th, 2009
0

Re: Optimizing this code

You unnecessary use the list.

python Syntax (Toggle Plain Text)
  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)
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 28th, 2009
0

Re: Optimizing this code

If you're really looking to get fancy use some list comprehension:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python/Twisted send data to specific client
Next Thread in Python Forum Timeline: Formatting Data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC