[英]reading log files in a loop and printing desired results (python)
I am trying to read 3 log files and use parsing to extract the requered information; 我正在尝试读取3个日志文件并使用解析来提取所需的信息; I need this code to run in a loop and obtain new lines if they meet requered parameters.
我需要此代码在循环中运行,并在满足要求的参数时获取新行。
I wrote the following code: 我写了以下代码:
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)
what I am trying to accomplish: 1. read the file. 我要完成的工作:1.读取文件。 2. search for relevant line.
2.搜索相关行。 3. if the information does not exist in the list, append to list.
3.如果列表中不存在该信息,请追加到列表中。 4. print line.
4.打印线。
I need help setting a while loop, and I am decently doing something wrong while comparing the line to the content of the list. 我需要设置while循环的帮助,在将行与列表内容进行比较时,我确实做错了。
UPDATE1: UPDATE1:
Ok so I managed to get my code to work by adding: 好的,所以我设法通过添加以下代码来使代码正常工作:
and line not in x_list:
to my original line: 到我的原始行:
if 'error' in line:
so now I got: 所以现在我得到了:
if 'error' in line and line not in x_list:
full code: 完整代码:
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)
it does what i need but i still need to run it in a loop, can anyone help me? 它可以满足我的需求,但我仍然需要循环运行,有人可以帮助我吗?
UPDATE2: managed to get it to work in a loop, if a new line is added and it meets the parsing parameters it will be printed. UPDATE2:设法使其循环运行,如果添加了新行并且满足解析参数,则将打印该行。
code: 码:
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)
The optimized approach: 优化方法:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.