2014-05-21 30 views
1

我有包含醃索引文本的列表,如下圖所示兩個不同的目錄,保存在.OUT格式的文件:的Python for循環都要經過目錄中的文件

(LP0 S'TCCTCTTGGAGCACCAGCTAATATTTCATCAGTATTCGCTGAATCTTCGGACATAGTTCA」 P1 aS'TTCGGACATAGTTCATTCATATTTATTTGCCCAATACCCGCACGAAGAAGCCTTGCAGAC「 P2 aS'AGAAGCCTTGCAGACACCGTGGCA」 P3 一個。

我試圖完成的任務是從犯罪嫌疑文本目錄中打開一個文件,並比較它使用python的difflib將其添加到源文本目錄中的每個文件,然後打印出一個數字,指示它們是否匹配,然後對可疑文本目錄中的其餘文件執行相同的操作。 (注:如果有人知道更詳細的方式來比較索引文本的兩個列表,我都聽過,但它遠不是優先級)

我目前的問題是用for循環來完成這個任務,它不起作用。我的意思是說,我可以循環瀏覽文件夾,並可以打印出文件夾名稱,但是文件本身的內容不會改變。循環目前只是多次比較每個目錄中的一個文件,我不知道如何解決它。

歡迎任何和所有建議,如果我的解釋已經足夠清楚,請隨時提出任何問題。

謝謝。另外,我知道這是一個常見問題,我盡力去查看以前的答案並應用他們所用的內容,但由於我不擅長編程,所以我正在努力做到這一點。

在此先感謝!

˚F

代碼如下:

import string 
import pickle 
import sys 
import glob 
import difflib 


sourcePath = 'C:\Users\User\Sou2/*.out' 
suspectPath = 'C:\Users\User\Susp2/*.out' 
list_of_source_files = glob.glob(sourcePath) 
list_of_suspect_files = glob.glob(suspectPath) 


def get_source_files(list_of_source_files): 

    for source_file_name in list_of_source_files: 
     with open(source_file_name) as source_file: 
      sourceText = pickle.load(source_file) 
     return sourceText 


get_suspect_files(list_of_suspect_files): 

    for suspect_file_name in list_of_suspect_files: 
     with open(suspect_file_name) as suspect_file: 
      suspectText = pickle.load(suspect_file) 
     return suspectText 


def matching(sourceText,suspectText): 

      matching = difflib.SequenceMatcher(None,sourceText,suspectText) 
      print matching.ratio() 


def main(): 

    for suspectItem in list_of_suspect_files: 
     suspectText = get_suspect_files(list_of_suspect_files) 
     print ('----------------SEPERATOR-----------------') 
     for sourceItem in list_of_source_files: 
      sourceText = get_source_files(list_of_source_files) 
      matching(sourceText,suspectText) 


main() 

當前的結果:

----------------SEPERATOR----------------- 
0.0 
0.0 
0.0 
----------------SEPERATOR----------------- 
0.0 
0.0 
0.0 
----------------SEPERATOR----------------- 
0.0 
0.0 
0.0 
----------------SEPERATOR----------------- 
0.0 
0.0 
0.0 

這應該是1.0其中一些爲我故意把匹配索引文本到文本系統。

+0

謝謝你的更新凱文! – FrankN

回答

2

您的函數get_source_filesget_suspect_files每個都包含循環,但是在循環的第一次迭代中返回。所以這就是爲什麼你的程序只查看每個列表中的第一個文件。

此外,這兩個函數中的循環由主函數中的循環複製。在你的主函數中,你永遠不會使用循環變量suspectItemsourceItem,所以這些循環只是多次執行相同的操作。

可能是,您很困惑yieldreturn,並以某種方式期待您的函數的行爲像生成器。

像這樣的東西應該工作

def get_text(file_name): 
    with open(file_name) as file: 
     return pickle.load(file) 

def matching(sourceText,suspectText): 
    matching = difflib.SequenceMatcher(None,sourceText,suspectText) 
    print matching.ratio() 

def main(): 
    for suspect_file in list_of_suspect_files: 
     print ('----------------SEPERATOR-----------------') 
     suspect_text = get_text(suspect_file) 
     for source_file in list_of_source_files: 
      source_text = get_text(source_file) 
      matching(source_text, suspect_text) 

main() 

注意,這重複每次迭代的源文本的加載。如果這很慢,並且文本不會太長以至於無法放入內存中,則可以將所有源文本和可疑文本存儲在列表中。

+0

非常感謝您的支持!我非常高興我可以哭泣,現在已經連續2天對此感到震驚......謝謝Stuart!這必須是高效的,用於比較的文件非常大,因此這將是完成任務的更好方式,謝謝! – FrankN