簡體   English   中英

TypeError: 'NoneType' 對象對於字數統計程序是不可迭代的

[英]TypeError: 'NoneType' object is not iterable for a word counting program

當我運行我當前的代碼時,我得到一個 TypeError: 'NoneType' object is not iterable。 具體來說,主函數中的 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()

提前致謝。 非常感激。

我假設 get words 沒有返回數組中的任何內容,因為您錯誤地附加了行。 在返回之前添加打印詞。 當您調用 get wordcount 函數時,它回復 none 類型是不可迭代的。 而不是words = words.line.split() ,使用 append 函數。 words.append(line.split())

try/except之后聲明函數getwordswords 如果引發IOException ,您只需return . 這將在失敗open返回None 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM