简体   繁体   English

使用.split python在文本文件中搜索关键字

[英]searching a text file for key words using .split python

I have a text file with chicken, recipe for chicken as line 1 and beef, beef recipe as line 2. 我有一个文本文件,其中有鸡肉,鸡肉食谱作为第1行以及牛肉,牛肉食谱作为第2行。

I can search for the beef or chicken recipe if the user only types beef or chicken. 如果用户只输入牛肉或鸡肉,我可以搜索牛肉或鸡肉食谱。 But I want to search for beef recipes if the user types in "I would like to search for beef recipes" or chicken.... 但是,如果用户输入“我想搜索牛肉食谱”或鸡肉,我想搜索牛肉食谱。...

I am trying to use .split but not quite there. 我正在尝试使用.split,但不完全使用。 Below is my code. 下面是我的代码。

while True:
    food = input ("What food? ")
    file = open("meals.txt", "r")
        line = file.readline()
        data = line.split(",")
        if data[0] == food:
            print(data[1])
    file.close()

You will want to make the "food" item into a list, and iterate over it. 您将需要将“食品”项放入列表,然后对其进行迭代。

food_type = food.split()
for type in food_type:
    if type == 'beef':
        #do something
    elif type == 'chicken':
        #do something else

Ideally you should be using elasticsearch for this. 理想情况下,您应该为此使用elasticsearch。 However if you want to do string parsing. 但是,如果要执行字符串解析。

while True:
food = input ("What food? ")
food_tokens = food.split()
file = open("meals.txt", "r")
    line = file.readline()
    data = line.split(",")
    for food_token in food_tokens:
        if data[0] == food_token:
            print(data[1])
file.close()

This checks for words "beef" and "chicken" and if input contains them, then it prints the data you want. 这将检查单词“ beef”和“ chicken”,如果输入包含它们,则将打印所需的数据。

First you'll need some sort of list of valid search words, in this case keywords = ['beef', 'chicken'] . 首先,您需要某种有效搜索词的列表,在这种情况下, keywords = ['beef', 'chicken'] Otherwise, if someone were to search for 'a chicken recipe' you might also get a hit on 'recipe' in 'a tasty beef recipe'. 否则,如果有人要搜索“鸡肉食谱”,那么“美味牛肉食谱”中的“食谱”也可能会获得成功。 Then you find all valid keywords in the user input, and then match the valid ones to the meals: 然后,在用户输入中找到所有有效的关键字,然后将有效的关键字与进餐匹配:

keywords = ['beef', 'chicken']
userinput = input("what food? ")

# generate a list of meals
with open("meals.txt", "r") as file:
  meals = file.readlines()

for word in userinput.split():
  if word in keywords:
    for meal in meals:
      if word in meal:
        # yay found one, store it or something

You can use a regex: 您可以使用正则表达式:

import re

foodType = re.search('(beef|chicken)', food)
if foodType is None:
    print 'beef or chicken, make your choice.'
else:
    print foodType.group(1) 
    """whatever you meant to do with beef or chicken instead.
       foodType.group(1) is 'chicken' or 'beef'
    """

In there if foodType is None, it means your user didn't type chicken or beef, at all. 如果foodType为None,则表示您的用户根本没有输入鸡肉或牛肉。 Otherwise foodType.group(1) is set to chicken, or beef, whichever one was entered. 否则,将foodType.group(1)设置为鸡肉或牛肉,以任何一个输入为准。 I just put a print, but from there you can do whatever you want with it. 我只是打印了一张照片,但您可以从那里随心所欲地打印。

file = open("txt", "r")
all_lines = file.readlines()

while True:
    food = raw_input ("What food? ")
    for line in all_lines:
        data = line.split(',')
        if data[0] == food:
            print "Found %s" % food
            print "data[1] is %s" % data[1]
            break
    else:
        print "Not found %s" % food

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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