I'm writing a program in python that will request a user to input a file name, open the file, and count the number of M's and F's and tally it as a ratio. I can get it to do that, and remove whitespace, but I can't figure out how to remove characters that are not M or F. I want to remove all incorrect characters and write them in a new file. Here's what I have so far
fname = raw_input('Please enter the file name: ') #Requests input from user
try: #Makes sure the file input is valid
fhand = open(fname)
except:
print 'Error. Invalid file name entered.'
exit()
else:
fhand = open(fname, 'r') #opens the file for reading
entireFile = fhand.read()
fhand.close()
entireFile.split() #Removes whitespace
''.join(entireFile) #Rejoins the characters
entireFile = entireFile.upper() #Converts all characters to capitals letters
males = entireFile.count('M')
print males
females = entireFile.count('F')
print females
males = float(males)
females = float(females)
length = males + females
print length
length = float(length)
totalMales = (males / length) * 1
totalFemales = (females / length) * 1
print "There are %", totalMales, " males and %", totalFemales, " in the file."