WEEK07: File I/O, string methods, top-down design
---------------------------------------------------------------
M: File Input/Output, string methods
REVIEW:
- for quiz 3, see /home/jk/inclass/q3.py
- last time we looked at /home/jk/inclass/coinflip.py
HINT for rain homework...
- write a function that takes two parameters, a string and a character,
and returns True if the character is in the string. For example,
lookFor("abcdefg", "x") would return False, and lookFor("we love CS", "e")
would return True
algorithm: search through string one char at a time
if we find the character, immediately return True
if we finish the search and haven't found the character, return False
# --------------------------------------------------- #
def lookFor(S, ch):
"""return True if ch in S"""
for letter in S:
if letter == ch:
# found it...so immediately return to caller
return True
# only gets here if for loop is done and char is not found
return False
# --------------------------------------------------- #
# Here's how most new programmers try to write that:
def lookFor(S, ch):
"""this function does NOT work"""
for letter in S:
if letter == ch:
return True
else:
return False
# see the difference??! what would lookFor("we love CS", "e") return???!
# --------------------------------------------------- #
FILE I/O:
- here's how to open a file for writing (note: myfile is a variable
name that I choose, and "newfile" is the name of the file to write to):
$ python
>>> myfile = open("newfile", 'w')
>>> myfile.write("write this to the file \n")
>>> myfile.write("and this.... \n")
>>> myfile.close()
- and here are the results:
$ cat newfile
write this to the file
and this....
- if you open a file for reading, use 'r' mode:
>>> infile = open("words.txt", 'r')
- note: infile is a variable of type file:
>>> type(infile)
<type 'file'>
- and it can be used as a sequence (in a for loop!):
>>> for line in infile:
... print line
...
happy
birthday
computer
smile
STRING METHODS:
- see http://docs.python.org/release/2.5.2/lib/string-methods.html
or type help(str) in a python interactive session to see a list
of string methods
- here are some examples that use string methods:
>>> mystring = " We Love COMPUTER science!! "
>>> mystring.upper()
' WE LOVE COMPUTER SCIENCE!! '
>>> mystring.lower()
' we love computer science!! '
>>> print mystring
We Love COMPUTER science!!
NOTE: mystring is not changed!!! These methods RETURN a new
string, but don't change the current/given string. If you
want to change your string, try this: mystring = mystring.upper()
>>> mystring.strip()
'We Love COMPUTER science!!'
>>> mystring.strip().lower()
'we love computer science!!'
NOTE: you can combine methods one after another!!
>>> mystring.strip().lower().split()
['we', 'love', 'computer', 'science!!']
>>> mystring.isdigit()
False
>>> mystring.isalpha()
False
YOUR TURN:
- how would you read in grades from a file formatted like this:
lisa :95
jeff :35
charlie :88
jonathan :97
rich :77
andy :70
doug :55
betsy :100
amanda :99
- let's write a main() function for this, without writing the
other functions:
def main():
grades = readFromFile("grades.txt")
ave = findAverage(grades)
print grades
print ave
- I don't know how exactly the functions readFromFile and findAverage
will work, but I have specified their parameters and what they
will return
- write main() and some "stub" functions and get this to work...