Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在下面的代码中使用get_close_匹配?_Python - Fatal编程技术网

Python 如何在下面的代码中使用get_close_匹配?

Python 如何在下面的代码中使用get_close_匹配?,python,Python,我正在尝试建立一个交互式词典。我使用get close匹配返回打字错误的结果。有人能指出我的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 =

我正在尝试建立一个交互式词典。我使用get close匹配返回打字错误的结果。有人能指出我的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")

要使其正常工作,您可以使用列表理解,并从数据库中获取密钥列表

query1 = cursor.execute("SELECT Expression from Dictionary")
keys = [b[0] for b in cursor.fetchall()]
然后,当您使用get_close_matches()时,您将执行以下操作:

get_close_matches(word, keys)

如果
results
为非空,
elif get\u close\u匹配项(word、cursor.stored\u results())
将永远不会执行。