如何在文本文件中查找特定单词?

2024-06-16 13:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何在不使用模块re的情况下在文本文件中查找特定的单词。 这是我当前的代码:

searchfile = open("Menu.txt", "r")
myList = []
cho = input("What cusine would you like to view the menu for")


for line in searchfile:
    myList.append(line+"\n")

splitted_line = line.split(',')
print(splitted_line[0])
indian = (myList[0])
mexican = (myList[1])
italian = (myList[2])

if 'mexican' in cho:
    print(mexican)
elif 'indian' in cho:
    print(indian)
elif 'italian' in cho:
    print(italian)


searchfile.close()

cho2 = input("Would you like to order now or wait till later")
if cho2 == 'now':
    import practiseassessment.py

if cho2 == 'later':
    print("Ok for more information contact hungry horse at 07969214142")

这是文本文件:

^{pr2}$

我只想在cho == indian时打印印度食品。 我希望文本文件是每道菜的新行


Tags: inyouforinputiflineindianprint
2条回答

首先,我仔细看了你的数据,我想你忘了一些逗号。所以我假设输入数据如下:

lamb curry , indian , chicken curry , indian , vegtable curry , indian
tacos , mexican fajitas , mexican nachos , mexican
margherita pizza , italian , vegetable pizza , italian , bbq chicken pizza , italian

有几种方法可以获得所需的输出。但是,我使用stringlist操作只是为了避免使用额外的Python模块,比如re。 我使用的唯一模块是sys模块,如果由于不均匀的列表长度而组合feal和origin失败,则中止执行以退出程序。但是,在编写专用函数时,可以通过使用适当的return语句来消除这个sys.ext()。因此,这个解决方案能够在不需要额外模块的情况下工作。我认为使用sys.exit()可以用来演示这个想法。在

如需进一步解释,请参阅代码中的注释:

^{pr2}$

如果用户想要indian进餐,输出文件如下所示:

lamb curry
chicken curry
vegtable curry

这是解决问题的一种方法。如您所见,您可以使用python的内置字符串完成所有操作。在

请注意,在比较搜索词和菜单项之前,请使用str.lower().strip()来规范化它们。这样可以得到更大的结果,而且不会因为用户输入额外的空间而惩罚用户,这通常是一件好事。在

# 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') 

输出示例:

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

相关问题 更多 >