简体   繁体   中英

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.

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. 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. 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.

First you'll need some sort of list of valid search words, in this case 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. Otherwise foodType.group(1) is set to chicken, or beef, whichever one was entered. 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

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