ecs10-11-13

ecs10-11-13 - 11/14/2007 Announcements ECS 10 11/13 Program...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 11/14/2007 Announcements ECS 10 11/13 Program 5 due tonight, 10pm Next midterm Mon Nov 19, material covered in class Wds Nov 21 will not be on final. Midterm is open book, open notes. Bring sample programs from class, your programs, etc. Example midterm (from last year) on class Web page. Questions About Elections What is PF? AI? Lib? Which gets the most votes? Why are there districts with no Republican or no Democratic candidates? D i did ? Replacing parts of strings Common problem with number strings (you might encounter in assignment) - there's commas inside them. We could write a function to remove the commas. You should all know how to do this. Or, we could see if there is an appropriate string method.... The replace method Google "Python string methods" Removing Empty Lines Our input file includes lots of annoying empty lines. An empty line consists of a singe newline "\n" "\ character. character Let's write a program that removes empty lines from any file. s = "Flinch" s = s.replace("Fl","Gr") 1 11/14/2007 Crashes if File Name Wrong The program has no way of checking whether the user typed the name of a real file except by trying to open it. But we never want to write programs that crash. crash 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 Python mechanism for handling user input that might crash the program: try: inFile i Fil = open(inFileName, " ") (i Fil N "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! How helper.py Works # Takes string s as input # Returns True if it can be converted to a float # Returns False if not def isFloat(s): try: float(s) # Try to do the conversion except: return False # Conversion failed! # Gets here if did not return already. return True When to Use Exceptions Use exceptions to handle input from the user, or sometimes from files, which you cannot control any other way. We have seen the most common scenarios: file names, and converting user-input strings to usernumbers. Otherwise exceptions should be rare. Most crashes are because there is a something wrong with your program. Fix the bug, don't bug, put it inside an exception. Back to Removing Blank Lines inStr = inFile.readline() # Read first line # Empty string means end of file while inStr != "": print inStr, # Lines have newline on end inStr = inFile.readline() # Read next line A typical loop that reads a file one line at a time. How do we recognize blank lines when we get to them? 2 11/14/2007 Whitespace Spaces look blank. Tabs look blank. '\t\n' ' \t\n` Both of these look like a blank line. Generically called whitespace. More String Methods >>> s = " \t\n" >>> t = s.strip() >>> t '' >>> s ' \t\n' >>> Strip method removes spaces, tabs, newlines. spaces, tabs, newlines. from beginning and end of string. Leaves Whitespace Inside s = "\n\n\nCats " "\ s.strip() s.strip() is "Cats" s = "Cats\n\n\nCats" "Cats\ nCats" S.strip() "Cats\ S.strip() is "Cats\n\n\nCats" nCats" In Our Program # Strip out leading and trailing whitespace testStr = inStr.strip() if testStr != "": print inStr, Writing the Output File outFile.write(inStr) Write out only the non-blank lines. y nonCan open any file for writing, so program won't crash if it does not exist. 3 ...
View Full Document

This note was uploaded on 04/14/2008 for the course ECS 10 taught by Professor Amenta during the Fall '07 term at UC Davis.

Ask a homework question - tutors are online