Review of some string/list/file functions/methods:
.split():
>>> s='Hi<bleep>there<bleep>Fred'
>>> things=s.split('<bleep>')
>>> print things
['Hi','there','Fred']
>>> another=s.split('<')
>>> print another
['Hi','bleep>there','bleep>Fred']
>>> t='Hi;there;Fred;'
>>> print t.split(';')
['Hi','there','Fred',''] # note the extra blank string at the end of the list
>>> v=' Hi there Fred '
>>> v.split()
['Hi','there','Fred']
.join():
>>> lst=['Hi','there','Fred']
>>> p=','.join(lst)
>>> print p
Hi,there,Fred
>>> print '\n'.join(lst) # notice how print displays strings
Hi
there
Fred
>>> '\n'.join(lst) # notice the difference between using print to display a string, and just asking the shell to display it (as it's doing here)
'Hi\nthere\nFred'
File reading/writing:
def FileReading(filename):
# read a file and return a list of lines in the file
try:
f=open(filename,'rU')
s=f.read()
f.close()
except:
s=''
lines=s.split('\n')
# remove any trailing blank lines
while len(lines)>0 and lines[-1]=='':
lines=lines[:-1]
return lines
def FileWriting(filename,stuff):
# write stuff (a string) to a file
try:
f=open(filename,'w')
f.write(stuff)
f.close()
except:
return False
return True
iHay erethay, edFray. adaYay andway oremay adayay.
3. I showed a file called name_grade.csv on the board on Friday. Download and look at this file using a text editor (not a spreadsheet program). Create a function called HighLow(filename), that will read this file (and any other file just like it) and print out the name and grade of the person with the highest and lowest scores. Like so:
>>> HighLow('name_grade.csv')
highest: Peter with 99
lowest: Ron with 23
4. Somewhat harder (though I did cover how to do this in class, using .split() and .sort()). Create the function called Rank(infile,outfile) that will read a file like the name_grade.csv file and re-order from highest to lowest grade, and then save the results to the file named in the second argument. So, a call to Rank('name_grade.csv','re-ranked.csv') would produce a file that looked like this (people with the same grade can be in any order):
Name,Grade Peter,99 Hermione,98 Voldy,49 Harry,49 Ron,23
5. (Challenge) Create the function CharRank(filename) that reads a file and returns the alphabet re-ordered in decreasing order of usage in the text. In other words, figure out how many times each letter was used in the text, and place the most used letter first in hte output string, and the least used (but used at least once) letter last. So if the file contained just the words: 'Hi there', then the output would be 'ehtri' because 'e' and 'h' are more frequently used than 't' or 'r' or 'i'. Run your function on the file harry.txt', and you should get 'aedryhtonmif'. Now run the function on 'BillOfRights.txt'... Look up Etaoin shrdlu.