python 2.7.8 TypeError:“NoneType”对象没有属性''

2024-10-03 17:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从sqllite数据库获取数据,它有数据。但我不知道为什么这是抛出错误。这个D = self.con.execute('select length from googlepagelength where urlid = {0}'.format(row[0])).fetchone()[0]正在打印3个值,但在下一次迭代中它会出错。在

代码:

def bm25scoreresultsfromGoogle (self, rows, wordids, k = 2.0, b = 0.75):
        # Find total number of documents in the collection
        #print 'row',rows
        #print 'wordids',wordids
        N = self.con.execute ('select count(*) from googleurllist').fetchone()[0]
        N = float(N)
        # Find the length for all documents in collection (not only the retrieved ones)
        ids = [id for id in self.con.execute ('select rowid from googleurllist')]
        alldoclengths = dict([(id[0], self.con.execute ('select length from googlepagelength').fetchone()[0]) for id in ids])
        # Calculate avgdl (average doc length)
        avgdl = 0.0
        for i in range(len(alldoclengths)):
            avgdl += alldoclengths.values()[i]
        avgdl = avgdl*1/len(alldoclengths)
        scores = dict([(id[0], 0.0) for id in ids])
        # Calculate |D| -> Document length
        i=0
        #print 'row',rows
        for row in rows:
            i=i+1
            #print i,'rowww',row[1]
            D = self.con.execute('select length from googlepagelength where urlid = {0}'.format(row[0])).fetchone()[0] # error appearing
            #print 'D',D  # 

            tempscore = 0.0
            for wordid in wordids:
                wordfreq = self.con.execute('select count (*) from googlewordlocation where urlid = %i and wordid = %i' %(row[0], wordid)).fetchone()[0]
                print 'wf',wordfreq
                num = wordfreq*(1.0 + k)
                den = b*D*1/avgdl
                den += (1.0 - b)
                den *= k
                den += wordfreq
                # Calculate the right part of the equation
                rscore = num*1/den
                # Find number of documents containing wordid
                nq = self.con.execute('select distinct urlid from googlewordlocation where wordid = %i' %wordid)
                nq = len([id for id in nq])
                # Calculate IDF
                IDF = log10((N - nq + 0.5)*1/(nq + 0.5))
                tempscore += IDF*rscore
            scores[row[0]] = tempscore
            #print 'IDF',scores[row[0]]
        return self.normalizescores(scores)

错误:

^{pr2}$

我做错什么了吗?在


Tags: theinfromselfidforexecutecon
1条回答
网友
1楼 · 发布于 2024-10-03 17:28:09

10.5.8 MySQLCursor.fetchone() Method

This method retrieves the next row of a query result set and returns a single sequence, or None if no more rows are available. By default, the returned tuple consists of data returned by the MySQL server, converted to Python objects. If the cursor is a raw cursor, no such conversion occurs; see Section 10.6.2, “cursor.MySQLCursorRaw Class”.

source: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html

您试图从NoneType检索信息,因为当没有更多的行可用时,函数将返回该信息。(到达查询结果的结尾)

一个快速但丑陋的“修复”:

D = self.con.execute('select length from googlepagelength where urlid = {0}'.format(row[0])).fetchone()
if D:
    D = D[0]

由于D与您期望的类型不同(如果有的话),这会失败,因此我不建议直接使用此代码,而只是作为如何修复错误的参考。在

相关问题 更多 >