简体   繁体   English

如何从用户输入中搜索单词列表的文本文件,并打印包含这些单词的行?

[英]how can i search a text file of list of words from user input and print the line which contains these words?

My query here is I want to search words from a text file and print those lines which contain those words. 我在这里的查询是我想从文本文件中搜索单词并打印包含这些单词的行。 All the words are given as user input. 所有单词均作为用户输入给出。 Somehow I reached till this point it's is giving output as nothing. 直到现在我一直以某种方式达到了无输出的目的。

def sip(x): 
    print("====Welcome to SIP log Debugger ==== ")
    file= input("Please Enter log File path: ")
    search = input("Enter the Errors you want to search for(seperated with commas):   ")
    search = [word.strip() for word in search.lower().split(",")]
    with open(file,'r') as f:
        lines = f.readlines()
        line = f.readline()
        for word in line.lower().split():
            if word in line:
                print(line),
                if word == None:
                    print('')

You're reading all the lines and saving them into a variable: 您正在读取所有行并将其保存到变量中:

lines = f.readlines()

Then you try to read one more line: 然后,您尝试再读一行:

line = f.readline()

But you've already read through the whole file, so there's nothing to read anymore, thus f.readline() returns '' . 但是您已经阅读了整个文件,因此不再需要读取任何内容,因此f.readline()返回'' Next you try to loop through each word in the line variable, which is just '' . 接下来,您通过在每个单词尽量循环line变量,这仅仅是''

Instead of all this, you should just loop through all the lines using for line in f: , so something like: 除了所有这些,您还应该for line in f:使用for line in f:遍历所有行,如下所示:

with open(file, 'r') as f:
    for line in f:
        line = line.lower()
        for word in search:
            if word in line:
                print(line)

I'm not sure what you're trying to do with the if word == None: , the word can never be None since line is a string and word is part of that string (you used line.split() ). 我不确定您要使用if word == None:做什么,因为line是一个字符串,而word是该字符串的一部分(您使用line.split() ),所以该单词永远不能为None

暂无
暂无

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

相关问题 如何从用户输入中搜索文本文件中的单词列表? - How can I search a text file for a list of words from user input? 如何从包含特定字母的列表中打印出单词? 我知道如何处理以特定字母开头但不以 BETWEEN 开头的单词 - How can I print out words from a list that contains a particular letter? I know what to do with words starting with specific letter but not in BETWEEN 列出文本文件中的单词并按输入字母搜索这些单词 - Make a list of words from text file and search these words by letters from input 如何获取计数器以从输入文本文件的每一行到输出文本文件的相应行上打印唯一单词的频率? - How to get Counter to print frequency of unique words from each line of the input text file to the corresponding line on the output text file? 如何创建一个字典,其中包含文本中的单词作为键和“它出现的子列表”作为值? - How can I create a dictionary that contains words from a text as keys and the "sublist in which it appears " as values? 如何从文本文件中打印随机单词 - How to Print Random Words from a Text File 如何从匹配文本列表中获得子字符串或2个单词? - How can I get substring or 2 words from the list on matching text? 如何使用python搜索大文本文件是否包含句子中的单词? - How to search if large text file contains words from sentence using python? 如何搜索地址集中是否包含某些特定单词 - How Can I search if a Address Set Contains some specific Words Python:如何在文本文件中搜索用户输入的包含整数的字符串? - Python: How can I search a text file for a string input by the user that contains an integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM