简体   繁体   English

在python中出现搜索项的打印行

[英]Print line in which search item occurs in python

I'm trying to get my program to print the line in which a search item occurs in Python. 我正在尝试让我的程序打印在Python中出现搜索项的行。 This is my current attempt: 这是我目前的尝试:

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

It keeps saying "printline" not defined. 它一直说“ printline”未定义。 I thought I defined it at the top. 我以为我在顶部定义了它。

Update: I removed "printline" as global variable and moved it to the beginning of a for loop. 更新:我删除了“ printline”作为全局变量,并将其移至for循环的开头。 Now the code prints every line of text (not just the lines that contain the search term). 现在,代码将打印文本的每一行(而不仅仅是包含搜索词的行)。 This is what I was trying to avoid and the reason I came up with the "printline" variable to begin with. 这就是我要避免的原因,也是我提出“ printline”变量的原因。

To clarify, I have tried: 为了澄清,我尝试过:

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

and I have tried: 我已经尝试过:

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

and both methods end up spitting out every line of the text file. 两种方法最终都会吐出文本文件的每一行。 Any ideas? 有任何想法吗? Thanks for everyone's help so far. 到目前为止,感谢大家的帮助。

You're using an equivalence marker (==) to set your printline variable. 您正在使用等价标记(==)设置打印线变量。

It should be "printline = False" to set, "printline == False" to compare 设置应该为“ printline = False”,进行比较为“ printline == False”

Try this: 尝试这个:

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

EDIT: We should be able to remove the need for the printline variable. 编辑:我们应该能够删除对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))

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

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