我正在嘗試讀取3個日誌文件並使用解析來提取重新請求的信息;我需要這個代碼在一個循環中運行,並且如果它們符合重新設置的參數,則獲得新的行。在循環中讀取日誌文件並打印所需的結果(python)
我寫了下面的代碼:
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)
你寫的'x_log =開放( 'x.txt')',但在這之後,你寫的'在x_srv_log行: ',你應該改爲'在x_log中輸入:' – Morb
已編輯!在我的原始代碼中沒有這個錯誤。 – Epsilon
我不明白你想做什麼:) – 2017-07-03 11:35:53