2014-05-21 31 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