-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