【问题标题】:Print line in which search item occurs in python打印在python中出现搜索项的行
【发布时间】:2018-01-26 17:49:03
【问题描述】:

我正在尝试让我的程序打印 Python 中出现搜索项的行。这是我目前的尝试:

search = input("Input your search term: ")  
found = 0
printline == False

with open ("search.txt", 'r') as data:
    for line in data: 
        if search.casefold() in line.casefold():
            found += line.casefold().count(search.casefold())  #adds the number of occurences within a line
            printline == True
        if printline == True:
            print(line)
        else: 
            printline == False

print("{} occurence(s) of the search item was/were found".format(found))

它一直说“打印线”未定义。我以为我在顶部定义了它。

更新:我将“printline”作为全局变量删除,并将其移至 for 循环的开头。现在代码打印每一行文本(不仅仅是包含搜索词的行)。这是我试图避免的,也是我想出“printline”变量开始的原因。

为了澄清,我试过了:

search = input("Input your search term: ")
found = 0


with open ("search.txt", 'r') as data:
    for line in data:
        if search.casefold() in line.casefold():
            found += line.casefold().count(search.casefold())  #adds the number of occurences within a line
            print(line)

print("{} occurence(s) of the search item was/were found".format(found)) 

我已经试过了:

search = input("Input your search term: ")
found = 0


with open ("search.txt", 'r') as data:
    for line in data:
        printline = False
        if search.casefold() in line.casefold():
            found += line.casefold().count(search.casefold())  #adds the number of occurences within a line
            printline = True
        if printline == True:
            print(line)
        else:
            printline = False

print("{} occurence(s) of the search item was/were found".format(found))

这两种方法最终都会吐出文本文件的每一行。有任何想法吗?感谢大家到目前为止的帮助。

【问题讨论】:

  • = 太多。做printline = False

标签: python full-text-search keyword-search


【解决方案1】:

您正在使用等价标记 (==) 来设置您的 printline 变量。

设置应该是“printline = False”,比较应该是“printline == False”

试试这个:

search = input("Input your search term: ")
found = 0
printline = False

with open ("search.txt", 'r') as data:
    for line in data:
        if search.casefold() in line.casefold():
            found += line.casefold().count(search.casefold())  #adds the number of occurences within a line
            printline = True
        if printline == True:
            print(line)
        else:
            printline = False

print("{} occurence(s) of the search item was/were found".format(found))

编辑:我们应该能够消除对 printline 变量的需求。

search = input("Input your search term: ")
found = 0

with open ("search.txt", 'r') as data:
    for line in data:
        if search.casefold() in line.casefold():
            found += line.casefold().count(search.casefold())  #adds the number of occurences within a line
            print(line) # This should only trigger if your if search.casefold() is in the line.casefold()

print("{} occurence(s) of the search item was/were found".format(found))

【讨论】:

  • 感谢您帮助我了解如何设置我的 printline 变量。但是,代码方法会打印出文本文件的每一行,而不仅仅是包含搜索项的行。这是我试图避免的,因为我的原始代码做了同样的事情。具体来说,我没有第二个“if”或“else”语句,我只是在“found +=...”之后添加了一行,上面写着“print(line)”。
  • 您在找到单行后将 printline 变量设置为 True,但是您从未将变量设置回 false。我已经编辑了我的答案以包含一些更简洁的代码
  • 就是这样。这个程序吐出文本文件的每一行。我想出“printline”变量的原因是为了防止这种情况发生。
  • 您可以将您的代码上传到 Github 并在此处或 PM 中链接吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 2017-08-10
  • 2011-06-14
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多