'int'对象没有属性'append'

时间:2022-04-20 08:48:55

When I run this code then following error is encountered, I am new to programming and I know I have bunch of useless arrays. I don't know where my error is as I have declared j as an array. I am completely out of ideas.

当我运行这个代码时,会遇到以下错误,我是编程新手,我知道我有很多无用的数组。我不知道我的错误在哪里,因为我已经声明了j是一个数组。我完全没有主意。

import pyodbc,nltk,array,re,itertools
cnxn = pyodbc.connect('Driver={MySQL ODBC 5.1 Driver};Server=127.0.0.1;Port=3306;Database=information_schema;User=root; Password=1234;Option=3;')
cursor = cnxn.cursor()
cursor.execute("use collegedatabase ;")
cursor.execute("select *  from sampledata ; ")
cnxn.commit()
s=[]
j=[]
x=[]
words = []
w = []
sfq = []
POS=[]
wnl = nltk.WordNetLemmatizer()
p = []
clean= []
l =[]
tupletolist= []
results = []
aux = []
regex = re.compile("\w+\.")
pp = []
array1=[]

f = open("C:\\Users\\vchauhan\\Desktop\\tupletolist.txt","w")
for entry in cursor:
    s.append(entry.injury_type),j.append(entry.injury_desc) 

def isAcceptableChar(character):
    return character not in "~!@#$%^&*()_+`1234567890-={}|:<>?[]\;',/."


from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
english_stops = set(stopwords.words('english'))
for i in range(0,200):
    j.append(filter(isAcceptableChar, j[i]))
    w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
    for j in range (0,len(w[i])):
        results = regex.search(w[i][j])
            if results:
                str.rstrip(w[i][j],'.')
for a in range(0 , 200):
    sfq.append(" ".join(w[a]))

from nltk.stem import LancasterStemmer
stemmer = LancasterStemmer()

for i in range (0,200):
    pp.append(len(w[i]))

for a in range (0,200):
    p.append(word_tokenize(sfq[a]))
    POS.append([wnl.lemmatize(t) for t in p[a]])
    x.append(nltk.pos_tag(POS[a]))
    clean.append((re.sub('()[\]{}'':/\-[(",)]','',str(x[a]))))
    cursor.execute("update sampledata SET POS = ? where SRNO = ?", (re.sub('()[\]{}'':/\-[(",)]','',str(x[a]))), a)

for i in range (0,len(array1)):
    results.append(regex.search(array1[i][0]))
    if results[i] is not None:
        aux.append(i)

f.write(str(w))

Exception:

例外:

Traceback (most recent call last):
  File "C:\Users\vchauhan\Desktop\regexsolution_try.py", line 37, in <module>
  j.append(filter(isAcceptableChar, j[i]))
AttributeError: 'int' object has no attribute 'append'

4 个解决方案

#1


2  

j has been used a a list as well as an integer. Use j only for integer name, name the list to something else.

j已经被使用了一个列表和一个整数。只对整数名称使用j,将列表命名为其他内容。

j.append(filter(isAcceptableChar, j[i]))    # j is not a list here,it is an int.
w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
for j in range (0,len(w[i])):               # here j is an int

#2


1  

Your indentation is broken, but it seems like it's this line that is the culprit:

你的压痕被打破了,但似乎这条线是罪魁祸首:

for j in range (0,len(w[i])):

The first time, j is an array, but then you hide it with the int j. It's hard to discover since what causes the error seems to happen after it, but since it's in a loop, that's not really true. Try renaming this integer.

第一次,j是一个数组,然后你用int j隐藏它,很难发现它是什么原因导致的,但是因为它是循环的,这不是真的。尝试重命名这个整数。

#3


1  

You seem to use the variable 'j' as a int-counter in the loop, i.e. the list 'j' is replaced by an int 'j' where you cannot append something. Solution: Rename the variables with more sophisticated names...

您似乎在循环中使用变量'j'作为一个int计数器,也就是说,list 'j'被一个int 'j'代替,在这里您不能附加任何东西。解决方案:用更复杂的名称重命名变量…

#4


1  

Take a close look at the following piece of your code:

仔细看看下面这段代码:

for i in range(0,200):
    j.append(filter(isAcceptableChar, j[i]))
    w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
    for j in range (0,len(w[i])):

Notice how you first call .append on j (which you had initialized with a list earlier), then use it as a loop variable nested in the same loop.

注意您如何第一次调用.append on j(您之前已经用一个列表初始化了它),然后将它作为嵌套在同一个循环中的循环变量。

Use better, meaningful variable names in your code to avoid this class of errors. Rename either the loop variable or the module-level list variable.

在代码中使用更好的、有意义的变量名来避免此类错误。重命名循环变量或模块级列表变量。

#1


2  

j has been used a a list as well as an integer. Use j only for integer name, name the list to something else.

j已经被使用了一个列表和一个整数。只对整数名称使用j,将列表命名为其他内容。

j.append(filter(isAcceptableChar, j[i]))    # j is not a list here,it is an int.
w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
for j in range (0,len(w[i])):               # here j is an int

#2


1  

Your indentation is broken, but it seems like it's this line that is the culprit:

你的压痕被打破了,但似乎这条线是罪魁祸首:

for j in range (0,len(w[i])):

The first time, j is an array, but then you hide it with the int j. It's hard to discover since what causes the error seems to happen after it, but since it's in a loop, that's not really true. Try renaming this integer.

第一次,j是一个数组,然后你用int j隐藏它,很难发现它是什么原因导致的,但是因为它是循环的,这不是真的。尝试重命名这个整数。

#3


1  

You seem to use the variable 'j' as a int-counter in the loop, i.e. the list 'j' is replaced by an int 'j' where you cannot append something. Solution: Rename the variables with more sophisticated names...

您似乎在循环中使用变量'j'作为一个int计数器,也就是说,list 'j'被一个int 'j'代替,在这里您不能附加任何东西。解决方案:用更复杂的名称重命名变量…

#4


1  

Take a close look at the following piece of your code:

仔细看看下面这段代码:

for i in range(0,200):
    j.append(filter(isAcceptableChar, j[i]))
    w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
    for j in range (0,len(w[i])):

Notice how you first call .append on j (which you had initialized with a list earlier), then use it as a loop variable nested in the same loop.

注意您如何第一次调用.append on j(您之前已经用一个列表初始化了它),然后将它作为嵌套在同一个循环中的循环变量。

Use better, meaningful variable names in your code to avoid this class of errors. Rename either the loop variable or the module-level list variable.

在代码中使用更好的、有意义的变量名来避免此类错误。重命名循环变量或模块级列表变量。