2017-02-04 178 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