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

时间:2017-07-03 11:23:42

标签: 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_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循环,并且在将行与列表内容进行比较时,我正在做错事。

UPDATE1:

好的,所以我设法通过添加:

让我的代码工作
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)

它做了我需要的但是我仍然需要在循环中运行它,任何人都可以帮助我吗?

UPDATE2: 设法让它在循环中工作,如果添加了一个新行并且它符合解析参数,它将被打印。

代码:

x_list = []
y_list = []
z_list = []
t = 1

while t == 1:
    x_log = open('x.txt','r')
    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','r')
    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','r')
    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)

1 个答案:

答案 0 :(得分:0)

优化方法:

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')