简体   繁体   English

在文件中查找字符串,追加到包含字符串的行的开头和结尾

[英]Find string in file, append to the beginning and end of line containing string

I have my script looking at a text file that contains a lot of text on a lot of lines. 我的脚本正在查看一个文本文件,该文件包含许多行上的大量文本。 I'm trying to get it so whenever the word "ERROR" is found, it appends to the beginning and end of the line containing "ERROR" 我正在尝试获取它,因此无论何时找到单词“ ERROR”,它都会附加到包含“ ERROR”的行的开头和结尾

So far I have a variable that returns a list of all lines that contain "ERROR" 到目前为止,我有一个变量,该变量返回包含“ ERROR”的所有行的列表

with open(infile, 'r') as f:
    targets = [line for line in f if "ERROR" in line]

so if I print targets it will return a list of all of the lines that contain "ERROR". 因此,如果我打印目标,它将返回包含“ ERROR”的所有行的列表。

targets[0] returns the first line etc. target [0]返回第一行,依此类推。

I've also been able to apply the formatting: 我还可以应用格式:

for i in targets:
    format = '<span style="background-color: #ff0000; color: #ffffff;">' + i + '</span>'
    formatted_line = i.replace(i, format)

But I can't seem to figure out how to have the script write a new file with all of the text including the formatting that was added to the start and end. 但是我似乎无法弄清楚该脚本如何编写一个新文件,其中包括添加到开头和结尾的格式在内的所有文本。

Just need a point in the right direction! 只需向正确的方向指出一点即可! Thanks 谢谢

EDIT: some clarification on the desired output: 编辑:对所需输出的一些说明:

original file: 原始文件:

log line of text here 在此处记录文本行

log line of text here 在此处记录文本行

error line of text here 错误文字行在这里

log line of text here 在此处记录文本行

error line of text here 错误文字行在这里

log line of text here 在此处记录文本行

new file: 新文件:

log line of text here 在此处记录文本行

log line of text here 在此处记录文本行

<span style="background-color: #ff0000; color: #ffffff;">error line of text here</span>

log line of text here 在此处记录文本行

<span style="background-color: #ff0000; color: #ffffff;">error line of text here</span>

log line of text here 在此处记录文本行

with open(infile, 'r') as IN, open('output.txt', 'w') as OUT:
    for line in IN:
        if "ERROR" in line:
            f = '<span style="background-color: #ff0000; color: #ffffff;">' + line + '</span>'
            OUT.write(f + '\n')
        else:
            OUT.write(line + '\n')

Just add a .split() function to the line in the if statement to convert it into an array rather than a string. 只需在if语句的行中添加.split()函数,即可将其转换为数组而不是字符串。

with open(infile, 'r') as f:
    targets = [line for line in f if "ERROR" in line.split()]

print(targets)

I think splitting the logic of reading and writing is more readable. 我认为将读写逻辑分开更容易理解。 And...readability is very important in coding. 而且...可读性在编码中非常重要。

I saved your text in a text file called text.txt and I saved the output in new_text.txt . 我将您的文本保存在一个名为text.txt的文本文件中,并将输出保存在new_text.txt I didn't know exactly what format you wanted for the result (mostly spacing), so you can play around with that to see what fits your needs. 我不知道您想要哪种格式的结果(主要是空格),因此您可以尝试使用哪种格式来满足您的需求。

from pathlib import Path

# Get stored text
text_fp = Path.home().joinpath('Desktop','text.txt')

lines = []
with open(text_fp,'r') as fp:
    for line in fp:
        if 'error' in line:
            line = '<span style="background-color: #ff0000; color: #ffffff;">{}</span>'.format(line)
        lines.append(line)

# New file name
new_text_fp = Path.home().joinpath('Desktop','new_text.txt')
with open(new_text_fp,'w') as fp:
    for line in lines:
        fp.write('{}\n'.format(line))

Input data: 输入数据:

log line of text here
log line of text here
error line of text here
log line of text here
error line of text here
log line of text here

Result: 结果:

log line of text here

log line of text here

<span style="background-color: #ff0000; color: #ffffff;">error line of text here
</span>
log line of text here

<span style="background-color: #ff0000; color: #ffffff;">error line of text here
</span>
log line of text here

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

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