-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")
我意识到,解决方案,但有一种方法可能不计数作为一个可选的挑战,我想知道应该怎么做 –
我添加了一个例子,而不计数方法来实现。 – Tristan