满足某些条件时替换列表中的元素

时间:2020-01-07 22:46:35

标签: python regex

我正在研究python脚本,目前在该脚本中,如果定义的文本文件中包含与列表匹配的某些词组,则会将其从文件中删除。

相关列表代码段如下:

replacements = {'pc-123456 alert good-name-here':'',
                'pc-123456 alert remove-name-here':'',
}

{内的前半部分是警报文件中的直接文本,而::处是从文件中清除文本(如果匹配)。目前,这可行。

我需要在脚本的替换列表中添加以下内容:

replacements = {'12-Dec-19 00:00 pc-123456 alert good-name-here':'',
                '12-Dec-19 00:01 pc-123456 alert remove-name-here':'',
                '12-Dec-19 00:01 pc-234567 alert remove-name-here':'',
}

但是我想删除所有定义为“在此处删除名称”的详细信息(包括日期/时间,设备名称等),即使警报将在2个以上的设备(例如pc- 123456,pc-2345678,pc-356435,pc-4563255)等。

如果脚本为相同的警报名称选择了不同的设备名称并删除时间戳(当前在替换列表中未定义),那么删除整个文本行的最简单方法是什么?

其余代码如下:

lines = []
with open('path/to/file.txt') as infile:
    for line in infile:
        for src, target in replacements.items():
            line = line.replace(src, target)
        lines.append(line)

with open('path/to/same/file.txt', 'w') as outfile:
    for line in lines:
        outfile.write(line)

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您知道一行的末尾是什么,可以执行以下操作:


to_remove_endings = ['to_remove_ending']
lines = []

with open('path/to/file.txt') as infile:
    for line in infile:
        next_line = line
        for ending in to_remove_endings:
            if line.rstrip().endswith(ending):
                next_line = '\n'
                break
        lines.append(next_line)

with open('path/to/same/file.txt', 'w') as outfile:
    for line in lines:
        outfile.write(line)

您还可以查找子字符串:

unwanted = ['substring 1', 'substring 2']
lines = []

with open('path/to/file.txt') as infile:
    for line in infile:
        next_line = line
        for substring in unwanted:
            if substring in line:
                next_line = '\n'
                break
        lines.append(next_line)

with open('path/to/same/file.txt', 'w') as outfile:
    for line in lines:
        outfile.write(line)
相关问题