2017-02-04 95 views
-1
Occurrences(inputFileNames, words, outputFileName) 

對於列表中inputFileNames的每個文件,輸出到 一個名爲outputFileName輸入 文件的名稱和每個詞列表中的words字符串出現的計數,數 單詞出現次數;如果任何輸入 文件無法讀取,請發出合適的錯誤消息 並跳過該文件。爲了增加樂趣,請不要使用 .count()內置功能。文件處理和文件

Occurrences(["sample1.txt","sample2.txt","sample3.txt"], ["why","you","fate","among"], "out.txt")

out.txt則包含:

File Name: why you fate among sample1.txt 3 0 0 0 sample2.txt 2 2 1 1 sample3.txt 0 3 0 0

什麼我走到這一步,是

def Occurrences(inputFileNames,words,outputFileName): 
    output = open(outputFileName,"a") 

    try: 
     for file in inputFileNames: 
      opned = open(file,"r") 
      print(opned) 
      counters = [0 for file in range (len(words))] 
      index = 0 
      for i in words: 
       for line in opned: 
        if i in line: 
         print("WORD",i,"LINE",line) 
         counters[index] += 1 
       index +=1 
      print(counters) 

    except IOError: 
     file.close() 
     print("*** Occurrences: File handle Error") 

回答

0

我也肯定會推薦使用的計數方法。在你的例子中,我無法真正看到你在哪裏寫結果到你的輸出文件,所以我會解釋一下可能的實現。然後

def occurrences(inputFileNames, words, outputFileName): 
    wordCount = {} 
    # This dictionary will hold our wordCount and be used for construnction of the output file 

    for file in inputFileNames: 
     # Iterate over the files 
     try: 
      with open(file, 'r') as infile: 
       content = infile.read().strip().split(" ") 
      # Declare entry to wordCount for file only if no IOError is raised 
      wordCount[file] = [0 for j in range(len(words))] 
      for i in range(len(words)): 
       # Instead of iterating over the contents manually, split them and use the count method 
       wordCount[file][i] = str(content.count(words[i])) 
     except IOError: 
      print("The file {} could not be read.".format(file)) 

    with open(outputFileName, 'w+') as outfile: 
     # Iterate over the wordCount dict and write the output 
     for i in wordCount.keys(): 
      outfile.write(i+" "+" ".join(wordCount[i])+"\n") 
occurrences(["book.txt"], ["Alice", "hole", "Rabbit"], "occ.txt") 

occ.txt包含:

book.txt 155 0 26 

要做到這一點不計數方法,一種可能的方式是通過元素遍歷內容列表元素遞增,如果字計數匹配元素。

for i in range(len(words)): 
    count = 0 
    for word in content: 
     if words[i] == word: 
      count += 1 
    wordCount[file][i] = str(count) 
+0

我意識到,解決方案,但有一種方法可能不計數作爲一個可選的挑戰,我想知道應該怎麼做 –

+0

我添加了一個例子,而不計數方法來實現。 – Tristan