读取循环中的日志文件并打印所需结果(python)

读取循环中的日志文件并打印所需结果(python),python,Python,我试图读取3个日志文件,并使用解析来提取重新查询的信息;我需要这段代码在循环中运行,如果它们满足重新查询的参数,就可以获得新行 我编写了以下代码: import os x_list = [] y_list = [] z_list = [] x_log = open('x.txt') for line in x_log: line = line.rstrip() if 'error' in line: x = line for x in x_lo

我试图读取3个日志文件,并使用解析来提取重新查询的信息;我需要这段代码在循环中运行,如果它们满足重新查询的参数,就可以获得新行

我编写了以下代码:

import os

x_list = []
y_list = []
z_list = []

x_log = open('x.txt')
for line in x_log:
    line = line.rstrip()
    if 'error' in line:
        x = line
        for x in x_log:
            if not x in x_log:
                x_list.append(x)
                print('ERROR1',x)

y_log = open('y.txt')
for line in y_log:
    line = line.rstrip()
    if 'error' in line:
        x = line
        for x in y_list:
            if not x in y_list:
                y_list.append(x)
                print('ERROR2',x)

z_log = open('z.txt')
for line in z_log:
    line = line.rstrip()
    if 'error' in line:
        x = line
        for x in z_log:
            if not x in z_list:
                z_list.append(x)
                print('ERROR3',x)
我正在努力实现的目标: 1.读取文件。 2.搜索相关行。 3.如果列表中不存在该信息,请附加到列表。 4.打印行

我需要帮助设置一个while循环,我在将行与列表内容进行比较时犯了一些错误

更新1:

好的,所以我通过添加以下内容使代码正常工作:

and line not in x_list:
我的原始行:

if 'error' in line:
所以现在我得到了:

if 'error' in line and line not in x_list:
完整代码:

x_list = []
y_list = []
z_list = []

x_log = open('x.txt')
for line in x_log:
    line = line.rstrip()
    if 'error' in line and line not in x_list:
        x_list.append(line)
        print('ERROR-X',line)

y_log = open('y.txt')
for line in y_log:
    line = line.rstrip()
    if 'error' in line and line not in y_list:
        y_list.append(line)
        print('ERROR-Y',line)

z_log = open('z.txt')
for line in z_log:
    line = line.rstrip()
    if 'error' in line and line not in z_list:
        z_list.append(line)
        print('ERROR-Z',line)
它做了我需要的,但我仍然需要在一个循环中运行它,有人能帮我吗

更新2: 设法使其在循环中工作,如果添加了新行并且它满足解析参数,则将打印它

代码:


优化方法:

def get_error_lines(fp, lines_set, suffix=''):
    ''' fp - file pointer; 
        lines_set - a set of unique error lines; 
        sufix - ERROR number(suffix) '''

    for line in fp:
        line = line.rstrip()
        if 'error' in line and line not in lines_set:
            lines_set.add(line)
            print('ERROR' + suffix, line)

# using set objects to hold unique items
x_set = set()
y_set = set()
z_set = set()

with open('x.txt', 'r') as x_log, open('y.txt', 'r') as y_log, open('z.txt', 'r') as z_log:
    get_error_lines(x_log, x_set, '1')
    get_error_lines(y_log, y_set, '2')
    get_error_lines(z_log, z_set, '3')

您编写
x\u log=open('x.txt')
,但之后,您为x\u srv\u日志中的行编写
,您应该为x\u日志中的行更改为
已编辑!我的原始代码中没有这个错误。我不明白你想做什么:)@MrGrj嗨,我有3个日志文件需要读取并在其中查找错误消息。然后我想将这些错误消息附加到一个列表中,并打印出来。然后重新阅读这三个文件,并将新行与列表内容进行比较,如果不在列表中,则追加并打印行;对于z_日志中的x:如果不是z_列表中的x:(…)。谢谢,但这太复杂了,我现在无法理解。
def get_error_lines(fp, lines_set, suffix=''):
    ''' fp - file pointer; 
        lines_set - a set of unique error lines; 
        sufix - ERROR number(suffix) '''

    for line in fp:
        line = line.rstrip()
        if 'error' in line and line not in lines_set:
            lines_set.add(line)
            print('ERROR' + suffix, line)

# using set objects to hold unique items
x_set = set()
y_set = set()
z_set = set()

with open('x.txt', 'r') as x_log, open('y.txt', 'r') as y_log, open('z.txt', 'r') as z_log:
    get_error_lines(x_log, x_set, '1')
    get_error_lines(y_log, y_set, '2')
    get_error_lines(z_log, z_set, '3')