不需要在python中计算空字符串(重复)

时间:2014-03-14 23:07:03

标签: python

该计划的目的是计算一个段落中的每个单词并记下频率。不幸的是,该程序还在计算空字符串。我的代码是:

def build_map( in_file, word_map ):
# Receives an input file and an empty dictionary


    for line in in_file:

    # Splits each line at blank space and turns it into
    # a list.
        word_list = line.split()



        for word in word_list: 
            word= word.strip().strip(string.punctuation).lower()#program revised           
            if word!='':

            # Within the word_list, we are stripping empty space
            # on both sides of each word and also stripping any
            # punctuation on both side of each word in the list.
            # Then, it turns each word to the lower case to avoid
            # counting 'THE' and 'the' as two different words.

                add_word( word_map, word)

如果有人能看一下这些代码并解释一下,为什么它还在计算空字符串,我真的很感激。除此之外,其他一切工作正常。谢谢(修改了代码,现在工作正常)。

1 个答案:

答案 0 :(得分:2)

您要检查单词是否为空,然后您正在删除空格和标点符号。颠倒这些操作的顺序。