我有包含醃索引文本的列表,如下圖所示兩個不同的目錄,保存在.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其中一些爲我故意把匹配索引文本到文本系統。
謝謝你的更新凱文! – FrankN