This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 11/10/2008 Announcements ECS 10
11/10 Program Program 5 due Wds, 10pm. Wds, Next Next midterm Mon Nov 17. Midterm Midterm is open book, open notes. Bring sample sample programs from class, your programs, etc. etc. Bring Scantron. Bring a Scantron. Removing Empty Lines
Sometimes Sometimes input files include annoying empty lines. An An empty line consists of a singe newline “\n” “\ character. character Let’s Let’s write a program that removes empty lines from any file. Let Let user enter name of file, then open it. Crashes Crashes if File Name Wrong
The The program has no way of checking whether the user typed the name of a real file except by trying to open it. But But we never want to write programs that crash. crash. Similar Similar to problem we had with getting numbers from the user; if they gave us a string that could not be converted, then the program would crash. Exceptions Exceptions
Python Python mechanism for handling user input that might crash the program: try: inFile = open(inFileName, "r") except: # Gets here if we cannot open the file print "Cannot find file",inFileName inFile = None Exceptions
try: # Command that might cause a crash except: # Executes this block if a crash would have # happened! 1 11/10/2008 Converting Strings
popStr = raw_input(‘Enter the population: ‘) try: pop = float(popStr) # Try conversion except: # Conversion failed! print ‘Not a number.’ pop = None if pop != None: # Put rest of program here. When to Use Exceptions
Use Use exceptions to handle input from the user, or sometimes from files, which you cannot control any other way. We We have seen the most common scenarios: file file names, and converting user-input strings userto numbers. Otherwise exceptions should be rare. Most Most crashes are because there is a something wrong with your program. Fix the bug, don’t Fix put it inside an exception. Back Back to Removing Blank Lines
inStr = None # Empty string means end of file while inStr != ‘’: inStr = inFile.readline() # Read next line print inStr, # Lines have newline on end Whitespace
Spaces Spaces look blank. Tabs Tabs look blank. '\t\n' ' \t\n‘ Both Both of these look like a blank line. Generically Generically called whitespace. inStr.split() inStr.split() splits on whitespace. whitespace. A typical loop that reads a file one line at a time. How do we recognize blank lines when we get to them? New New String Method
>>> s = ‘ \t\n’ >>> t = s.strip() >>> t '' >>> s ' \t\n' >>> Strip Strip method removes spaces, tabs, newlines. spaces, tabs, newlines. from beginning and end of string. Leaves Leaves Whitespace Inside
s = "\n\n\nCats " s.strip() is s.strip() is “Cats” s = "Cats\n\n\nCats" s.strip() "Cats\ s.strip() is "Cats\n\n\nCats" nCats" 2 11/10/2008 In Our Program
# Strip out leading and trailing whitespace testStr = inStr.strip() # If nothing is left, don’t print the line. if testStr != ‘’: print inStr, Writing the Output File
outFile.write(inStr) Write out only the non-blank lines. nonCan Can open any file for writing, so program won’t crash if it does not exist. 3 ...
View Full Document
This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.
- Winter '07