TypeError:“NoneType”对象不可用于字数计数程序

2024-09-29 22:20:26 发布

您现在位置: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()

提前谢谢。非常感谢。在


Tags: thetointxtfordictionaryisdef
2条回答

您可以在函数getwords内的getwordstry/except块之后声明。如果IOException被引发,您只需return。这将在失败的open上返回None。将words = []移到try/except之前。在

这段代码现在应该可以工作了,没有异常。在

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()

我假设get words没有返回数组中的任何内容,因为您不正确地附加了行。在返回之前添加一个打印单词。当您调用get wordcount函数时,它以none类型不可编辑的方式进行答复。使用append函数代替words = words.line.split()words.append(line.split())

相关问题 更多 >

    热门问题