此代码用于在一个简单的列表上完美运行,但由于我一直试图在数据库上运行它,因此在我的double for循环中出现StopIteration错误。 编辑:现在我的问题是我只是没有得到任何类型的错误,命令行是空的,当我运行它。当我在数据库而不是简单列表上试用时,为什么我的代码不能在Visual Studio上运行?
这里是我的代码:
# -*- coding: utf-8 -*-
import pyodbc
import re
from difflib import SequenceMatcher
[x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')]
# Connection to accdb
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\\Users\\alice\\Desktop\\lexique3.accdb;'
)
cnxn = pyodbc.connect(conn_str)
crsr = cnxn.cursor()
# Put all words into a list
crsr.execute("select unites_lexicales from Mot where cat_grammat='Verbe' or 'Nom' or 'Adjectif' or 'Adverbe'")
resultat = crsr.fetchall()
words = [row[0] for row in resultat]
#print(words)
# Remove unwanted characters and put radicals into a list
radicals = []
motifp = "^(an|re|dé|dés)"
motifs = "(ist|man|er|an|able|asyon)$"
for word in words :
word = re.sub(motifp, '', word)
word = re.sub(motifs, '', word)
radicals.append(word)
#print(radicals)
# Match radicals and put words into new lists
ratio = 0.6
n = len(radicals)
result = []
used_js = []
for i in range(n) :
if i in used_js:
continue
matches = [words[i]]
js = (x for x in range(n) if x != i and x not in used_js)
for j in js : # Previous error : StopIteration
if SequenceMatcher(None, radicals[i], radicals[j]).ratio() >= ratio :
matches.append(words[j])
used_js.append(j)
result.append(matches)
print(result)
预期的结果如下(我以前用一个简单的列表了):
['flore', 'fleur', 'fleuriste'], ['remaniement', 'remanier', 'manier', 'maniable'], ['désaimer', 'aimer', 'aimant'], ['mêler', 'emmêler', 'désemmêler']
已经分别尝试了代码的每一部分,一切正常除了for循环之外,还有其他优点 关于为什么我不能运行它以及我如何解决这个问题的任何线索都会非常有帮助。 非常感谢!
你可以发布一个堆栈跟踪? –
@LaurentLAPORTE如果使用print_stack()的方式工作,则在运行时不会显示任何内容。 –
@LaurentLAPORTE Visual Studio告诉我绝对没有错误,但代码仍然不会运行。 –