问题
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."
回答1:
Use a regular expression to extract all characters that are not M or F:
import re
remainder = re.sub(r'M|F', '', entireFile)
with open('new_file', 'wb') as f:
f.write(remainder)
回答2:
the easiest way is to use regex:
import re
data = re.findall(r'[FM]', entirefile)
and if you use r'[FMfm]' you don't need to upper case all the file, the regex will catch all upper and lower case.
and this will return to you all the F's and M's , and no need to remove white spaces at all.
example:
entirefile = "MLKMADG FKFLJKASDM LKMASDLKMADF MASDLDF"
data = ['M', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'M', 'F']
and you can do whatever you want with this list.
hope this helps.
回答3:
m,f,other = [],[],[]
for ch in entierFile:
if ch == "M":m.append(ch)
elif ch == "F":f.append(ch)
else: other.append(ch)
print len(m) + " Males, "+len(f)+" Females"
print "Other:",other
来源:https://stackoverflow.com/questions/22770959/removing-characters-from-a-txt-file-using-python