#Programmer: Sriram Pemmaraju #Date: March 13, 2012 #Version 2 # Takes a line s and replaces all punctuation marks given # in the list punctuationMarks by blanks; returns the modified list def filterOutPunctuation(punctuationMarks, s): for mark in punctuationMarks: s = s.replace(mark, " ") return s # Open the file "War and Peace" fin = open("smallWar.txt", "r") wordList = [] # List of all non-letter characters, We will replace these characters in each line by blanks punctuationMarks = map(chr, range(0, ord("A")) + range(ord("Z")+1, ord("a")) + range(ord("z")+1, 127)) # Loop that processes each line of the file for line in fin: newLine = filterOutPunctuation(punctuationMarks, line) wordList = wordList + newLine.split() #Close the input file fin.close() #Block of code that produces output fout = open("dictionary2.txt", "w") for word in wordList: fout.write(word+"\n") fout.close()