簡體   English   中英

如何在不區分用戶輸入的情況下進行python搜索

[英]How to make a python search with user entry case insensitive

嗨,我有一個運行良好的python搜索,然后有一個tkinter GUI可以接受用戶輸入。 然后,用戶輸入用於使用python查詢數據庫。 它根據輸入將記錄匹配在一起,例如,當我需要它可以將藍色與藍色匹配時,它將藍色與藍色匹配。有人知道如何做到這一點,這是我已經擁有的示例(注意是假想的情況);

def Search():
global E1, E2, E3, E4, file


    #Open the file in read mode
    file = sqlite3.connect('H:\\matching.db')

    #Welcome message
    print ("Please type in the information required")


    window = Tk()
    window.title ("Search for matches")

    def QuitSearch():
       window.destroy()


   #Create text that will be shown to the user
   window.configure(bg="red")
   Label(window, text="Please enter the colour of your hair").grid(row=0)
   Label(window, text="Please enter the colour of your eyes").grid(row=1)
   Label(window, text="Please enter your gender").grid(row=2)
   Label(window, text="Please enter your shoe size").grid(row=3)


   #Create where the user will input
   E1= Entry(window)
   E2= Entry(window)
   E3= Entry(window)
   E4= Entry(window)

   #Assigning the input boxes to area on the screen
   E1.grid(row=0, column=1)
   E2.grid(row=1, column=1)
   E3.grid(row=2, column=1)
   E4.grid(row=3, column=1)

   button = Button(window, text = "Submit information", command=Submit, fg="yellow", bg="black")
   button.grid(row=3, column=2, padx = 5)


   quitbutton = Button (window, text ="QUIT", command=QuitSearch, fg ="red")
   quitbutton.grid(row=3, column=3, padx=5)







#The submit function allows the data inserted by the user in Search to be submitted and to search the database    
def Submit():
    global E1, E2, E3, E4, file

    #Retaining user input
    eyecolour = E1.get()
    haircolour = E2.get()
    gender = E3.get()
    shoesize = E4.get()



    #The search
    cursor = file.execute ('''SELECT ID, forename, surname, FROM people
    WHERE eyecolour =? and haircolour=? and gender=? and shoesize=? ''', (eyecolour, haircolour, gender, shoezize)) 

    window=Tk()
    def QuitOutputScreen():
    window.destroy()

    for row_number, row in enumerate(cursor):
        Label(window, text ="ID = "+ str(row[0])).grid(row=1, column = row_number)
        Label(window, text ="Forename = "+str(row[1])).grid(row=2, column = row_number)
        Label(window, text ="Surname = "+(row[2])).grid(row=3, column = row_number)


    Label(window, text ="Search complete ").grid(column=11)

    quitbutton = Button (window, text ="QUIT", command=QuitOutputScreen, fg ="red")
    quitbutton.grid(row=11, column=11, padx=5)


file.close()

這看起來更像是一個涉及DBMS(在這種情況下為sqlite)的問題,而不是Python本身。

您可以在sqlite中使用不區分大小寫的搜索查詢來解決該問題。 您可以在如何在比較字符串時將Sqlite3設置為不區分大小寫的地方找到一些說明

在您的python代碼中,您可以使用字符串lower和upper函數使用所有小寫或大寫字母來存儲和搜索,或者可以使用正則表達式和re.IGNORE_CASE進行搜索。

對於數據庫查詢,您可以將所有內容存儲為固定大小寫,也可以告訴數據庫引擎忽略大小寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM