我正在试着建立一个交互式的字典。我使用get close matches返回拼写错误的结果。有人能指出我的elif出了什么问题吗?我是python的新手
import mysql.connector
import difflib
from difflib import SequenceMatcher
from difflib import get_close_matches
con = mysql.connector.connect(
user = "ardit700_student",
password = "ardit700_student",
host = "108.167.140.122",
database = "ardit700_pm1database",
)
word = input("Enter the word: ")
cursor = con.cursor() #create object to navigate through the page
query = cursor.execute("select * from Dictionary where Expression = '%s'" %word) #use cursor to execute the query
results = cursor.fetchall() # store all the results in a variable
if results:
for result in results:
print(result)
elif get_close_matches(word, cursor.stored_results()):
print(results)
else:
print("word not found")发布于 2020-06-23 03:41:35
要使其正常工作,您可以使用列表理解,并从db获取键的列表。
query1 = cursor.execute("SELECT Expression from Dictionary")
keys = [b[0] for b in cursor.fetchall()]然后,当您使用get_close_matches()时,您将执行以下操作:
get_close_matches(word, keys)https://stackoverflow.com/questions/61200878
复制相似问题