4

当我运行此代码时遇到以下错误,我是编程新手,我知道我有一堆无用的数组。我不知道我的错误在哪里,因为我已经声明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))

例外:

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

4 回答 4

7

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
于 2012-08-09T06:20:58.830 回答
3

仔细查看以下代码:

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])):

请注意您是如何第一次调用.appendon j(您之前已使用列表初始化的),然后将其用作嵌套在同一循环中的循环变量。

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

于 2012-08-09T06:22:06.323 回答
2

您似乎在循环中使用变量“j”作为整数计数器,即列表“j”被替换为整数“j”,您无法在其中附加某些内容。解决方案:用更复杂的名称重命名变量...

于 2012-08-09T06:17:23.360 回答
1

您的缩进被破坏了,但似乎是这条线是罪魁祸首:

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

第一次j是一个数组,但是你用 int 隐藏它j。很难发现导致错误的原因似乎发生它之后,但是由于它处于循环中,因此这不是真的。尝试重命名这个整数。

于 2012-08-09T06:15:17.990 回答