我正在用python编写一个程序,该程序将要求用户输入文件名,打开文件并计算M和F的数量并计算出它的比率。我可以做到这一点,并删除空格,但我不知道如何删除不是M或F的字符。我想删除所有不正确的字符,并将它们写入一个新的文件。这里是我到目前为止使用Python删除txt文件中的字符
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."
为什么不能在文件内容重复一次进行,每一字符的动作?例如,如果当前字符是M或F,则向变量添加一个。否则,将其从当前文件中移除并附加到新文件。 –
我对改进代码和理解的建议:1)'split'不会删除所有空白,只是(有效)换行符。 2)你打开两次'fhand',这是多余的,我想可能会把原来的'fhand'打开;请参阅http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python寻找不涉及打开文件两次的解决方案,或者只是弄清楚如何使用您创建的第一个“fhand”。 3)在这种情况下,将大部分代码放在一个巨大的'else'中是不必要的,因为如果遇到异常,'exit()'。 –