TypeError:对于单词计数程序,'NoneType'对象不可迭代

时间:2015-06-24 23:57:53

标签: python

当我运行当前代码时,我得到一个TypeError:'NoneType'对象不可迭代。具体来说,main函数中的mostword = wordcount(wordlist)第62行,以及我在第19行的wordlist中为x添加的位置。你能否帮我弄清楚我哪里出错了?

def getwords():

#function to get words in the input file
try:
    fp=open("sample.txt",'r')
except IOError:
    print('Unable to open file')
    return
words=[]
#read the file line by line
for line in fp:
    #convert each line into words with space as delimiter
    words=words+line.split()
return words


def wordcount(wordlist):

#function to count words in the file
#worddic is dictionary to store words frequency
worddic=dict()
for x in wordlist:
    #convert word to lowercase to ignorecase
    t=x.lower()
    if(t not in worddic):
        worddic[t]=0
    worddic[t]=worddic[t]+1
max=-1
t=''
for x in worddic:
    if(worddic[x]>max):
        max=worddic[x]
        t=x
return t

def letters(wordlist,lettercount):

#function to count letters in the file
for x in wordlist:
    #For each word in the list
    t=x.lower()
    for y in t:
        #for each letter in the word
        if(not (y in lettercount)):
            #if the letter is not in dictionary add it
            #and set frequency to zero
            lettercount[y]=0
        #increment the frequency of letter in dictionary
        lettercount[y] = lettercount[y]+1

def createoutput(lettercount,wordlist,mostword):

#creates an empty file 'statistics.txt'
try:
    fout=open("statistics.txt",'w+')
except IOError:
    print('Unable to create file')
fout.write('Number of words in the file are '+str(len(wordlist))+'\n')
fout.write('Most repeated word in the file is '+mostword+'\n')
for x in lettercount:
    #write to the file 'statistics.txt'
    fout.write(x+' appeared in the file for '+str(lettercount[x])+'   times \n')

def main():

wordlist=getwords()
#lettercount is a dictionary with letters as keys
#and their frequency in the input file as data
lettercount=dict()
mostword=wordcount(wordlist)
letters(wordlist,lettercount)
createoutput(lettercount,wordlist,mostword)
main()

提前致谢。非常感激。

2 个答案:

答案 0 :(得分:-1)

我假设得到的单词没有返回数组中的任何内容,因为你错误地追加了这些行。在返回之前添加打印文字。当你调用get wordcount函数时,它回复没有类型是不可迭代的。而不是words = words.line.split(),使用append函数。 words.append(line.split())

答案 1 :(得分:-1)

在<{em> words块后,您在函数getwords 中声明try/except。如果引发IOException,则只需return。这将在失败的None上返回open。将words = []移至try/except

之前

此代码应该现在,离开return words不会出现异常。

def getwords():

#function to get words in the input file
    words = []
    try:
        fp = open("sample.txt", "r")
    except IOError:
        return words
#read the file line by line
    for line in fp:
    #convert each line into words with space as delimiter
        words=words+line.split()
    return words


def wordcount(wordlist):

#function to count words in the file
#worddic is dictionary to store words frequency
    worddic=dict()
    for x in wordlist:
    #convert word to lowercase to ignorecase
        t=x.lower()
        if(t not in worddic):
            worddic[t]=0
        worddic[t]=worddic[t]+1
    max=-1
    t=''
    for x in worddic:
        if(worddic[x]>max):
            max=worddic[x]
            t=x
    return t

def letters(wordlist,lettercount):

#function to count letters in the file
    for x in wordlist:
    #For each word in the list
        t=x.lower()
        for y in t:
        #for each letter in the word
            if(not (y in lettercount)):
            #if the letter is not in dictionary add it
            #and set frequency to zero
                lettercount[y]=0
        #increment the frequency of letter in dictionary
            lettercount[y] = lettercount[y]+1

def createoutput(lettercount,wordlist,mostword):

#creates an empty file 'statistics.txt'
    try:
        fout=open("statistics.txt",'w+')
    except IOError:
        print('Unable to create file')
    fout.write('Number of words in the file are '+str(len(wordlist))+'\n')
    fout.write('Most repeated word in the file is '+mostword+'\n')
    for x in lettercount:
    #write to the file 'statistics.txt'
        fout.write(x+' appeared in the file for '+str(lettercount[x])+'   times \n')

def main():

    wordlist=getwords()
    #lettercount is a dictionary with letters as keys
    #and their frequency in the input file as data
    lettercount=dict()
    mostword=wordcount(wordlist)
    letters(wordlist,lettercount)
    createoutput(lettercount,wordlist,mostword)
main()