| | |
Optimizing this code
![]() |
•
•
Join Date: May 2009
Posts: 70
Reputation:
Solved Threads: 2
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.
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)
def columns(infile, outfile): f = open(infile,'r') o = open(outfile,'w') col = int(raw_input('Please select a column (starting at 0) from you in file, %s:' % (infile))) temp = [] #Store an empty list for line in f: if not line.strip(): #If empty line, strip the new line character and remaining space continue else: line = line.split() #Split lines so that they can be operated on via list operations temp.append(line[col]) #Write the user-specified column into the empty list, "temp o.write('\n'.join(temp)) print "See %s" % (outfile) f.close() o.close()
Last edited by shoemoodoshaloo; May 26th, 2009 at 11:23 am.
•
•
Join Date: May 2009
Posts: 74
Reputation:
Solved Threads: 1
this isnt that big of a deal, but you could add
this way, you can eliminate the "starting at 0" part and they will enter "1" for the first column.
col -= 1 after this: Python Syntax (Toggle Plain Text)
col = int(raw_input('Please select a column (starting at 0) from you in file, %s:' % (infile)))
Last edited by billymcguffin; May 26th, 2009 at 11:28 am.
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
* 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)
def columns(infile, outfile): f = open(infile,'r') o = open(outfile,'w') col = raw_input('Please select a column from your input file, %s:' % (infile)) col = int(col) - 1 temp = [] for line in f: line = line.strip() if line: # Split each line into columns line = line.split() # Write contents of user-specified columns to temp temp.append(line[col]) o.write('\n'.join(temp)) print "See %s" % (outfile) f.close() o.close()
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
You unnecessary use the list.
python Syntax (Toggle Plain Text)
def columns(infile, outfile): f = open(infile,'r') o = open(outfile,'w') col = raw_input('Please select a column from your input file, %s:' % (infile)) col = int(col) - 1 for line in f: line = line.strip() if line: # Split each line into columns line = line.split() # Write contents of user-specified columns to temp o.write(line[col]) o.write("\n") f.close() o.close() # this is the actual flushing print "See %s" % (outfile)
If you're really looking to get fancy use some list comprehension:
python Syntax (Toggle Plain Text)
def columns(infile, outfile): f = open(infile,'r') o = open(outfile,'w') # col = raw_input('Please select a column from your input file, %s:' % (infile)) col = int(col) - 1 o.write('\n'.join([line.strip().split()[col] for line in f if line != '\n'])) # f.close() o.close() # this is the actual flushing print "See %s" % (outfile)
Last edited by jlm699; May 28th, 2009 at 9:57 am.
![]() |
Similar Threads
- help Optimizing code (Java)
- code optimization ... (C++)
Other Threads in the Python Forum
- Previous Thread: Python/Twisted send data to specific client
- Next Thread: Formatting Data
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas import input ip itunes java leftmouse line linux list lists logging loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wxpython xlib






