简体   繁体   中英

Python 3 finding multiple words in a .csv file

Hi I am creating a troubleshooting program and I have a partial working solution, if a user types in a single word problem the word is found in the csv then the correct advice is output. However the problem is if multiple words are input by a user nothing is found and no advice is output at all, the program stops.

import csv
import webbrowser

print("Hello! Welcome to trouble shooter 2.0")

def menu():
    try:
        Phone = int(input("""What is your operating system?
    Please choose the corresponding number:
    1) iOS
    2) Android
    3) Other
    > """))

        if Phone == 1:
            print("Thank you iOS user")
        elif Phone == 2:
            print("Thank you Android user")
        elif Phone == 3:
            print("Thank you")
        else:
            print("please find out")
            exit()
    except ValueError:
        print("please enter a numerical input")
        menu()

menu()


task2 = open ("problem list.csv")
Problem = input ("""Please enter the issue you wish to resolve
>""")
KeyWords = Problem.split()
reader = csv.reader(task2, delimiter=',')

for row in reader:
    if Problem == row[0]:
            print(row[1])
    else:
        print("we do not have an answer for this")


helpful = input("""


Was this helpful?""").lower()
if helpful[0] == 'n':
    google = input("in this case, please input your issue, for a google   search: ")
    webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=%s'% google)

elif helpful[0] == 'y':
    print("""
You are welcome""")
    exit()

the issue is with if Problem == row[0]: , Problem is the return value of the split function which is a list .So you should check every value in the list which means you should use if row in Keywords : .

I did not tested it yet theoretically it should work !

I think you forgot this

KeyWords = Problem.split()

Actually you can use KeyWords instead of Problem ,because it seems that row[0] is a word,and the type of KeyWords now is list ,so why don't you try this:

    if row[0] in KeyWords:
            print(row[1])

Try to debug it and you will find what's wrong with your code.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM