【问题标题】:searching a text file for key words using .split python使用 .split python 在文本文件中搜索关键词
【发布时间】:2016-05-19 12:10:04
【问题描述】:

我有一个包含鸡肉的文本文件,第 1 行是鸡肉食谱,第 2 行是牛肉、牛肉食谱。

如果用户只输入牛肉或鸡肉,我可以搜索牛肉或鸡肉食谱。但是如果用户输入“我想搜索牛肉食谱”或鸡肉,我想搜索牛肉食谱......

我正在尝试使用 .split 但不完全在那里。下面是我的代码。

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()

【问题讨论】:

  • 如果您知道第一个单词是关键字,您可以执行if keyword in food: 之类的操作来检查关键字是否在输入短语中的任何位置。
  • 问题是如何搜索文本,还是如何获取关键字?
  • 使用这段代码stackoverflow.com/questions/6531482/… 来搜索输入短语是否有任何出现在list1 = ['chicken','beef']中的单词
  • 我会建议 - file = open("meals.txt", "r") 这一行做外部循环并检查循环中的中断条件
  • 很抱歉,这个问题的规范不完整。您必须定义什么是单词(或者这与什么是分隔符相同),或者如果您想接受单词作为子字符串,则作为替代。示例:我想要鸡蛋、牛肉、小牛肉或鹅(sep 是逗号)或相反的守卫者想要鸡肉食谱

标签: python file search text


【解决方案1】:

您需要将“食物”项制作成一个列表,并对其进行迭代。

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

【讨论】:

    【解决方案2】:

    理想情况下,您应该为此使用 elasticsearch。但是如果你想做字符串解析。

    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()
    

    这会检查单词“beef”和“chicken”,如果输入包含它们,那么它会打印你想要的数据。

    【讨论】:

      【解决方案3】:

      首先,您需要某种有效搜索词的列表,在本例中为 keywords = ['beef', 'chicken']。否则,如果有人要搜索“鸡肉食谱”,您可能还会在“美味牛肉食谱”中找到“食谱”。然后你在用户输入中找到所有有效的关键字,然后将有效的关键字与餐点匹配:

      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
      

      【讨论】:

        【解决方案4】:

        您可以使用正则表达式:

        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'
            """
        

        如果 foodType 为 None,则表示您的用户根本没有输入鸡肉或牛肉。否则 foodType.group(1) 设置为鸡肉或牛肉,以输入的为准。我只是打印了一张,但从那里你可以用它做任何你想做的事情。

        【讨论】:

          【解决方案5】:
          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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-07
            • 1970-01-01
            • 1970-01-01
            • 2015-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多