简体   繁体   English

如何在文本文件中搜索单词并用 Python 打印部分行?

[英]How to search for word in text file and print part of line with Python?

I'm writing a Python script.我正在编写一个 Python 脚本。 I need to search a text file for a word and then print part of that line.我需要在文本文件中搜索一个单词,然后打印该行的一部分。 My problem is that the word will not be an exact match in the text file.我的问题是这个词在文本文件中不会完全匹配。

For example, in the below text file example, I'm searching for the word "color=" .例如,在下面的文本文件示例中,我正在搜索单词"color="

Text File ex:文本文件例如:

ip=10.1.1.1 color=red house=big
ip=10.1.1.2 color=green house=small
ip=10.1.1.3 animal = dog house=beach
ip=10.1.1.4 color=yellow house=motorhome

If it finds it, it should print to a new text file "color=color" , not the whole line.如果找到它,它应该打印到一个新的文本文件"color=color" ,而不是整行。

Result Text File ex:结果文本文件例如:

color=red
color=green
color=yellow

My code:我的代码:

for line_1 in open(file_1):
    with open(line_1+'.txt', 'a') as my_file:
        for line_2 in open(file_2):
            line_2_split = line_2.split(' ')
            if "word" in line_2:
                if "word 2" in line_2:
                    for part in line_2:
                        line_part = line_2.split(): #AttributeError: 'list' object has no attribute 'split'
                        if "color=" in line_part():
                            print(line_part)

I believe I need to use regular expressions or something like line.find("color=") , but I'm not sure what or how.我相信我需要使用正则表达式或类似line.find("color=") ,但我不确定是什么或如何使用。

Question : How do I search a text file for a word (not an exact match) and the print only a specific part of each line?问题:如何在文本文件中搜索一个词(不是完全匹配)并只打印每行的特定部分?

Here's one way - split each line by spaces, then search each part for "color=":这是一种方法 - 用空格分割每一行,然后在每个部分中搜索“color=”:

with open("textfile.txt") as openfile:
    for line in openfile:
        for part in line.split():
            if "color=" in part:
                print part

Well, it may not be much, but you could always use regex:好吧,它可能不多,但你总是可以使用正则表达式:

m = re.search(r'(color\=.+?(?= )|color\=.+?$)', line)
if m:
    text = m.group() # Matched text here

暂无
暂无

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

相关问题 如何在文本文件中搜索单词并用 Python 打印整行? - How to search for word in text file and print the entire line with Python? 如何在文件的每一行中搜索单词并在 python 中打印它的下一个值 - How to search for the word in each line of a file and print the next value of it in python 如何在文本文件中查找一个单词并打印 Python 下一行中的另一个单词 - How to find a word in a text file and print another that is in the next line in Python 搜索文件中的单词并打印python中出现该单词的行号 - search a word in a file and print the line number where that word occurs in python 搜索文本文件并仅将行的一部分打印到Python中的多个txt文件中? - Search a text file and print only a part of line to multiple txt files in Python? 在python中按行搜索和打印单词 - Search and print word in line in python 如何在Python中的文本文件中搜索特定单词 - How to search a text file for a specific word in Python 我们如何在Python中打印文本文件中出现单词的行号? - How do we print the line numbers in which a word appears from a text file in Python? 在文本文件中搜索单词,然后从文本文件中打印下一个单词? - Search a text file for a word then print the next word from text file? 如何在文本文件中搜索特定名称,并使用 Python 打印整行? - How would I search a text file for a specific name, and print the whole line with Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM