试图在两个函数中使用相同的文件,但无法看到两个函数的输出?

时间:2014-08-28 06:13:02

标签: python-2.7

我正在尝试运行以下代码。但是我没有得到我对term_double_score()函数调用的输出。我真的很感激为什么会发生这种情况以及如何查看函数调用的输出?

  def term_double_score(sent_file):
    scores_double = {} # initialize an empty dictionary with double phrases
    for line in sent_file:
        term, score  = line.split("\t")  # The file is tab-delimited. 
        if " " in term: 
            scores_double[term] = int(score)
    print "Len_double:",len(scores_double.items())



def term_single_score(sent_file):
    scores_single = {} # initialize an empty dictionary with single phrases
    for line in sent_file:
        term, score  = line.split("\t")  # The file is tab-delimited. 
        scores_single[term] = int(score) 
    print "Len_single:",len(scores_single.items()) 


sent_file = open("some.txt")


term_single_score(sent_file)
term_double_score(sent_file)

1 个答案:

答案 0 :(得分:0)

  1. 在@ jonrsharpe的解决方案中,代码为single_score读取并处理文件一次。在使用double_score进行第二次处理之前,请使用sent_file.seek(0)重置文件指针,以便代码可以重新读取该文件。

  2. 以下解决方案读取整个文件并对其进行缓冲。 Single_score基于文件数据的副本运行。然后再次在副本上运行double_score

  3. 如果文件很小,后一种解决方案是好的;系统有足够的内存将其保存在内存中。对于巨大的文件,解决方案#1是优越的。

    def term_double_score(sent_file):
        scores_double = {} # initialize an empty dictionary with double phrases
        for line in sent_file:
            term, score  = line.split("\t")  # The file is tab-delimited. 
            if " " in term: 
                scores_double[term] = int(score)
        print "Len_double:",len(scores_double.items())
    
    
    
    def term_single_score(sent_file):
        scores_single = {} # initialize an empty dictionary with single phrases
        for line in sent_file:
            term, score  = line.split("\t")  # The file is tab-delimited. 
            scores_single[term] = int(score) 
        print "Len_single:",len(scores_single.items()) 
    
    
    # read file and buffer it
    sent_file = list( open('zoot.dat') )
    
    term_single_score(sent_file)
    term_double_score(sent_file)