2017-09-03 71 views
0

我正在通過一些文件搜索某些信息並且想要統計出現的次數。計數循環運行

第一次循環和計數器工作得很好,但所有其他結果都是「0」。

for c, info in enumerate(searchKeys): # loop over searchKeys list 
    counter = 0 # set counter 
    for line in currentFile: # loop over lines in current file 
     if info in line: # search for info in line 
      counter += 1 # count +1 if info found 

    out.write( "\nInfo: " + str(info) + "\n" + 
       "Occurrence: " + str(counter) + "\n") 

回答

0

你需要回到文件的開頭

counter = 0 # set counter 
currentFile.seek(0) # add this line 

詳情file_seek

+0

作品般的魅力!感謝您的鏈接 – FuzzyTemper