简体   繁体   English

如何在文本文件中搜索特定名称,并使用 Python 打印整行?

[英]How would I search a text file for a specific name, and print the whole line with Python?

My code is currently:我的代码目前是:

def GrabCPUInfo():
    with open("cpu_list.txt", "r") as file:
        line = file.readlines()
        if cpu in line:
            print(line)
        else:
            print("Incorrect information")

My issue is that it just keeps printing out "Incorrect information" instead of printing out the whole line that includes the cpu name.我的问题是它只是不断打印出“不正确的信息”,而不是打印出包含 cpu 名称的整行。

Let's say I have a file cpu_list.txt with the value假设我有一个带有值的文件cpu_list.txt

CPU 1: Example
CPU 2: Example
CPU 3: Example

You could do something like你可以做类似的事情

with open('cpu_list.txt','r') as f:
    # Read content as well removing \n
    content = [line.strip() for line in f.readlines()]

    # print(content)
    # ['CPU 1: Example', 'CPU 2: Example', 'CPU 3: Example']
    for line in content:
        if 'CPU 1' in line:
            print(line)
        else:
            print('Invalid Info')
        break

The output输出

CPU 1: Example

readlines() returns a list of strings where each element of the list is an entire line in the text file. readlines()返回一个字符串列表,其中列表的每个元素都是文本文件中的整行。 For example, using readlines on this text file...例如,在此文本文件上使用readlines ...

bob
joe
emily

would create the list ['bob\\n', 'joe\\n', 'emily\\n']将创建列表['bob\\n', 'joe\\n', 'emily\\n']

Unless cpu matches an entire line exactly (including the newline character), using in like this won't work.除非cpu完全匹配整行(包括换行符),否则像这样使用in是行不通的。 You can still use in on the individual strings to test if the string contains cpu .您仍然可以in单个字符串上使用in来测试字符串是否包含cpu Try something like this:尝试这样的事情:

def GrabCPUInfo():
    with open("test.txt", "r") as file:
        lines = file.readlines()
        for line in lines:
            if cpu in line:

I removed the else block because it would just print "Incorrect information" over and over again for every line that didn't have the correct string.我删除了else块,因为它只会为没有正确字符串的每一行一遍又一遍地打印“不正确的信息”。

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

相关问题 如何在文本文件中搜索单词并用 Python 打印部分行? - How to search for word in text file and print part of line with Python? 如何在文本文件中搜索单词并用 Python 打印整行? - How to search for word in text file and print the entire line with Python? 如何允许用户访问 python 中文件中的特定行? - How would I allow a user to access a specific line in a file in python? 如何在文本文件中搜索术语,然后用python中的新行替换整行? - How to search for a term in a text file then replace that whole line with a new line in python? 如何在 python 中打印文本文件的特定部分? - How can I print specific parts of a text file in python? 如何让python打印整个文件而不是单行 - how to make python print the whole file instead of just the single line 我将如何在txt文件中搜索特定单词,并阅读该单词所在的行及其下一行 - How would I search for a specific word in a txt file and read the line it is on and the one below it 如何读取文本文件中的行并替换 Python 中的整行? - How to read line in text file and replace the whole line in Python? 如何逐行读取文件并仅在python中打印具有特定字符串的行? - How do I read a file line by line and print the line that have specific string only in python? 如何从 python 中的文本文件中删除特定行 - How do I remove a specific line from a text file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM