2014-03-31 40 views
0

我正在用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." 
+1

为什么不能在文件内容重复一次进行,每一字符的动作?例如,如果当前字符是M或F,则向变量添加一个。否则,将其从当前文件中移除并附加到新文件。 –

+0

我对改进代码和理解的建议:1)'split'不会删除所有空白,只是(有效)换行符。 2)你打开两次'fhand',这是多余的,我想可能会把原来的'fhand'打开;请参阅http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python寻找不涉及打开文件两次的解决方案,或者只是弄清楚如何使用您创建的第一个“fhand”。 3)在这种情况下,将大部分代码放在一个巨大的'else'中是不必要的,因为如果遇到异常,'exit()'。 –

回答

1

使用正则表达式来提取是不是M或F的所有字符:

import re 
remainder = re.sub(r'M|F', '', entireFile) 
with open('new_file', 'wb') as f: 
    f.write(remainder) 
+0

afaik他不想只是丢弃无效的字符... –

+0

@JoranBeasley谢谢你指出。我编辑了我的答案 – spinlok

+0

+1我误解了你原来的答案:P –

2

最简单的方法是使用正则表达式:

import re 
data = re.findall(r'[FM]', entirefile) 

,如果你使用r'[FMfm]'你不需要为大写的所有文件,正则表达式将捕获所有大小写。

这将回到你所有的F'sM's,并且根本不需要删除white spaces

例如:

entirefile = "MLKMADG FKFLJKASDM LKMASDLKMADF MASDLDF" 
data = ['M', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'M', 'F'] 

,你可以做任何你想要与此列表。

希望这有助于。

1
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