2013-10-29 70 views
1

試圖編寫一個代碼,將在文本文件中找到所有的某種類型的字符 對於元音它會找到所有數量的a,但贏得不要通過文字來閱讀電子書。幫幫我?循環內循環不重新循環閱讀文件Python3

def finder_character(file_name,character): 

    in_file = open(file_name, "r") 

    if character=='vowel': 
     brain_rat='aeiou' 
    elif character=='consonant': 
     brain_rat='bcdfghjklmnpqrstvwxyz' 
    elif character=='space': 
     brain_rat='' 
    else: 
     brain_rat='[email protected]#$%^&*()_+=-123456789{}|":?><,./;[]\''  

    found=0 
    for line in in_file: 
     for i in range (len(brain_rat)): 
      found += finder(file_name,brain_rat[i+1,i+2]) 


    in_file.close() 
    return found 

def finder(file_name,character): 
    in_file = open(file_name, "r") 
    line_number = 1 
    found=0 
    for line in in_file: 
     line=line.lower() 
     found +=line.count(character) 
    return found 
+0

DEF取景器(file_name中,字符): in_file中=開放(FILE_NAME, 「R」) LINE_NUMBER = 1 實測值= 0 用於in_file中線:) 線= line.lower( 發現+ =行.count(字符) 返回發現 – WildCat

+0

當您不使用'line'時,爲什麼'finder_character'在in_file'的循環中循環? – jwodder

+0

使用行[: - 1]? – WildCat

回答

2

如果你想使用你的原代碼,您必須將文件名傳遞給finder()功能,並打開該文件存在,每個燒焦你的測試。

原因是文件對象(in_file)是一個生成器,而不是一個列表。發電機的工作方式是,每次調用方法時它都會返回下一個項目。當你說

for line in in_file: 

for ... in語句調用in_file.next()只要next()方法「申報表」(實際使用關鍵字yield,但不想想現在)的值。當發電機不再返回任何值時,我們說發電機已耗盡。您不能重複使用耗盡的發電機。如果你想重新開始,你必須建立一個新的發電機。

我讓自己重寫你的代碼。這應該會給你想要的結果。如果有什麼不清楚的地方,請問!

def finder_character(file_name,character): 

    with open(file_name, "r") as ifile: 
     if character=='vowel': 
      brain_rat='aeiou' 
     elif character=='consonant': 
      brain_rat='bcdfghjklmnpqrstvwxyz' 
     elif character=='space': 
      brain_rat=' ' 
     else: 
      brain_rat='[email protected]#$%^&*()_+=-123456789{}|":?><,./;[]\'' 

    return sum(1 if c.lower() in brain_rat else 0 for c in ifile.read()) 

測試。TXT:

eeehhh 
iii!# 
kk ="k 
oo o 

輸出:

>>>print(finder_character('test.txt', 'vowel')) 
9 
>>>print(finder_character('test.txt', 'consonant')) 
6 
>>>print(finder_character('test.txt', 'space')) 
2 
>>>print(finder_character('test.txt', '')) 
4 

如果您有了解return線路出現問題,應該倒着讀,像這樣:

Sum this generator: 
    Make a generator with values as v in: 
     for row in ifile.read(): 
      if c.lower() in brain_rat: 
       v = 1 
      else: 
       v = 0 

如果你想要了解更多關於發電機的信息,我建議使用Python Wiki page

2

這似乎是你想要做的finder_character。我不知道你爲什麼需要finder

在python中,你可以遍歷iterables(比如字符串),所以你不需要做range(len(string))

for line in in_file: 
    for i in brain_rat: 
     if i in line: found += 1 

有出現在你的代碼的一些其他古怪太:

  • 您打開(和遍歷)文件的兩倍,但只能關閉一次。
  • line_number從不使用
  • 你得到文件中每行的文件中的字符總數,所以總數將大大增加。

這可能是一個更安全的版本,with open...一般比open()... file.close()更好,因爲你不必擔心錯誤處理和關閉儘可能多的。我添加了一些評論來幫助解釋你正在嘗試做什麼。

def finder_character(file_name,character): 
    found=0 # Initialise the counter 
    with open(file_name, "r") as in_file: 
     # Open the file 
     in_file = file_name.split('\n') 

     opts = { 'vowel':'aeiou', 
       'consonant':'bcdfghjklmnpqrstvwxyz', 
       'space':'' } 
     default= '[email protected]#$%^&*()_+=-123456789{}|":?><,./;[]\'' 

     for line in in_file: 
      # Iterate through each line in the file 
      for c in opts.get(character,default): 
       With each line, also iterate through the set of chars to check. 
       if c in line.lower(): 
        # If the current character is in the line 
        found += 1 # iterate the counter. 
    return found # return the counter