[Solved] How to find specific word in a text file?


This is one way to approach the problem. As you can see, you can do everything with python’s builtin strings.

Note that use str.lower().strip() to normalize the search terms and the menu items before comparing them. That will give more generous results and not punish users for inputting extra spaces, and is usually a good thing.

# First parse you input file and create the menu list
with open("food.txt", "r") as foodfile:
    menu_items=" ".join(foodfile.readlines()).strip().split(',')
menu_items = [item.strip() for item in menu_items]

# menu_items is now the list ['lamb curry', 'indian', 'chicken curry' ... etc

# I made the next part into a loop, so you can test several search terms.
# instead of searching on user input, you can split the menu into several lists
# but in most real world cases that is not really useful. With this approach
# users can search for "pizza" or "chicken" in addition to "mexican", "italian" etc. 

while True:
    cho = input("What cusine would you like to view the menu for? ").lower().strip()
    if not cho:  
        break  # break the loop if user submits an empty search string
    search_results = [food for food in menu_items if cho in food.lower()]
    print('\nFound %d items on the meny matching "%s"' % (len(search_results), cho))
    print(', '.join(search_results))

print('\ngoodbye') 

example output:

What cusine would you like to view the menu for? indian

Found 3 items on the meny matching “indian” indian, indian vegtable
curry, indian tacos

What cusine would you like to view the menu for? mexican

Found 3 items on the meny matching “mexican” mexican fajitas, mexican
nachos, mexican margherita pizza

What cusine would you like to view the menu for? pizza

Found 3 items on the meny matching “pizza” mexican margherita pizza,
italian vegetable pizza, italian bbq chicken pizza

2

solved How to find specific word in a text file?