String/List/File Exercises

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


Exercises:

1. Create a short file called "fred.txt"  with about 5 lines of text (in a text editor like gedit or emacs or nano or notepad).  Create a function called wc(filename) that is given a filename, and will calculate the number of characters, the number of words and the number of lines inside that file, and returns a list of those 3 numbers.  As a test, download the file: harry.txt.and test the function on it by calling wc('harry.txt').  It should return: [36, 7, 4] (or something close).  Now try it on the two files (English words and Bill of Rights) on the homepage.  Notice how fast Python processes text.

2. Igpay Atinlay.  Convert the words in a text file to Pig Latin (easier than translating it into Mandarin).  Look up what the translation rules are and implement them.  Create the function igpay(infile,outfile) that takes two filenames, and reads infile and writes outfile translating to Pig Latin along the way.  So if you run igpay('harry.txt','arryhay.txt') and display the file 'arryhay.txt', you should see:
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.