Python 如何使用cursor.fetchall和pyodbc从数据库中列出行?
我有一个脚本,可以在一个简单的列表上运行。它从单词列表中删除一些不需要的字符,使它们相互匹配,并返回几个相似单词的列表(比率为0.6) 但现在我需要它来处理Access数据库。 我想,如果我在crsr.fetchall()上做一个for循环,并将所有项目放入一个列表(“单词”),那么它可以像以前一样工作。不幸的是,它没有,我真的不明白 这是我的密码:Python 如何使用cursor.fetchall和pyodbc从数据库中列出行?,python,database,cursor,pyodbc,fetchall,Python,Database,Cursor,Pyodbc,Fetchall,我有一个脚本,可以在一个简单的列表上运行。它从单词列表中删除一些不需要的字符,使它们相互匹配,并返回几个相似单词的列表(比率为0.6) 但现在我需要它来处理Access数据库。 我想,如果我在crsr.fetchall()上做一个for循环,并将所有项目放入一个列表(“单词”),那么它可以像以前一样工作。不幸的是,它没有,我真的不明白 这是我的密码: # -*- coding: utf-8 -*- import pyodbc import re from difflib import Sequ
# -*- 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;')
result1 = crsr.fetchall()
words = []
for item in result1 :
words.append[item]
print(words)
在这一点上,我得到了一个错误:
TypeError: 'builtin_function_or_method' object is not subscriptable
我尝试使用范围进行迭代:
crsr.execute('SELECT unites_lexicales FROM Mot;')
result = crsr.fetchall()
words = []
for i in range(0, len(result)) :
words.append(result[i])
print(words)
但我得到了一个项目清单,看起来像这样,一点也不令人满意:
[('anbaglé', ), ('anfoutan', ), ('òrdinè', ), ('alakous', ), ('ayen', ), ('anmè', ), ('antòtiyé', ),...]
下面是在一个简单列表中完美运行的其余代码:
radicals = []
motifp = "^(re|em|dés)"
motifs = "(iste|ment|er|ant|able)$"
for word in words :
word = re.sub(motifp, '', word)
word = re.sub(motifs, '', word)
radicals.append(word)
print(radicals)
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:
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', 'aimant', 'aimer'], ['désemmêler', 'emmêler', 'mêler']]
下面是我之前通过在一个简单列表上测试得到的结果:
radicals = []
motifp = "^(re|em|dés)"
motifs = "(iste|ment|er|ant|able)$"
for word in words :
word = re.sub(motifp, '', word)
word = re.sub(motifs, '', word)
radicals.append(word)
print(radicals)
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:
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', 'aimant', 'aimer'], ['désemmêler', 'emmêler', 'mêler']]
我一定是把整个光标部分搞错了,我真的不明白它是怎么工作的。。。谢谢你的帮助 您可以使用索引对每一行进行索引。此行只有一列,因此可以使用0。您还可以通过名称对其进行索引
# ...
crsr.execute('SELECT unites_lexicales FROM Mot;')
result = crsr.fetchall()
words = []
for row in result:
# words.append(row['unites_lexicales'])
words.append(row[0])
print(words)
# ...
您还可以使用列表理解来获取第一列
# ...
crsr.execute('SELECT unites_lexicales FROM Mot;')
result = crsr.fetchall()
# words = [row['unites_lexicales'] for row in result]
words = [row[0] for row in result]
print(words)
# ...
我只是碰巧发现了这篇文章&我想我会完成它的。我也有同样的问题。使用pyodbc时,正确的语法是Row.ColumnName而不是Row['ColumnName']。奇怪,但在您的第一个代码段中,您使用的是words.append[item]。你应该使用words.append(item)。append是一个方法。好的,谢谢,但是有什么方法可以将此列中的项目放入列表中吗?从fetchall()返回的行的行为类似于元组,但是您也可以按名称访问列。在循环中尝试words.append(item['unites\u lexicales'])或words.append(item[0])。这里有一些很好的例子。谢谢@Kyle,但它不起作用。。。我编辑了我的帖子,所以你可以显示结果,这和我尝试使用range时一样。谢谢!当我单独尝试时,它是有效的:我有我的单词列表,我也有我的部首列表,但当我尝试所有东西时,我什么都没有得到。没有错误信息,没有文字,只有黑屏。。。有什么线索吗?当我尝试使用['unites\u lexicales']时,我得到以下错误:TypeError:行索引必须是整数,而不是str代码的哪一部分被破坏了?这似乎与你原来的问题有所不同。您是否从sql查询中获取数据?在分别测试代码的每个部分之后:我获取数据,我有我的单词列表和部首列表。我认为这与最后一部分有关,但这真的很奇怪,因为它仍然可以很好地处理简单的列表!