Python 将来自日志文件不同行的三个字符串合并为一行
Python join three strings from different lines from log file into one line
我需要将来自不同行的三个不同字符串连接成一行。
我的日志文件如下所示:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 6, Error: No such member
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>401700
2020-07-03 15:21:58,962 ERROR [AIF]:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 7, Error: The member of the Account dimension is not base level.
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWRA;<Entity Currency>;A1399;[ICP None];[None];[None];[None];[None];44984167.990000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A1399
2020-07-03 15:21:58,962 ERROR [AIF]:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 15, Error: The member of the Account dimension is not base level.
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A2090;[ICP None];[None];[None];[None];[None];0.270000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A2090
错误:
Line: 6, Error: No such member | >>>>>>401700 | Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
下面是我的代码,分几行写的:
def main():
fo = open("C:\Users\yannis\py_script\FMC_1198.log", "r", encoding="ISO-8859-1")
ofile = open("C:\Users\yannis\py_script\out_log.txt",'a', newline='')
vinter = ""
vline = ""
vmember = ""
f1 = fo.readlines()
for x in f1:
res = x.partition('Line:')[2]
if len(res) > 0:
vline = res
continue
res = x.partition('>>>>>>')[2]
if len(res) > 0:
vmember = res
continue
res = x.partition('Actual;')[2]
if len(res) > 0:
vinter = res
continue
linha = vinter + vline + vmember
if len(linha) > 0:
print(linha)
ofile.write(linha)
continue
fo.close()
ofile.close()
非常感谢任何帮助!
出现这种情况是因为字符串末尾有\n
,你应该写一个辅助函数
def trimNewlines(st):
return st.replace("\n")
或者
def trimNewlines(st):
return st.rstrip("\n")
如果您只想删除字符串右侧的换行符而不是中间或左侧的换行符
我在想类似下面的事情:
new_log = str()
with open("old_log.txt", 'r') as input_file:
lines = [line[37:-1] for line in input_file.readlines()]
lines = [l for (i,l) in enumerate(lines) if i%4!=3]
remastered = list()
i = 0
while i + 2 < len(lines):
remastered.append(f"{lines[i]} | {lines[i+2]} | {lines[i+1]}")
i += 3
new_log = '\n'.join(remastered)
with open("new_log.txt", 'a') as input_file:
input_file.write(new_log+'\n')
试试这个,使用 zip
+ list splicing
# pre-processing, to remove un-wanted lines & starting character's
lines = []
for l in fo.readlines():
_, y = l.split("[AIF]:")
if y.strip():
lines.append(y)
with open("test.txt", 'w') as f:
# list splicing with zip, job done..
lines = [f"{a} | {b} | {c}\n"
for a, b, c in zip(lines[0::3], lines[2::3], lines[1::3])]
f.writelines(lines)
我需要将来自不同行的三个不同字符串连接成一行。 我的日志文件如下所示:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 6, Error: No such member
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>401700
2020-07-03 15:21:58,962 ERROR [AIF]:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 7, Error: The member of the Account dimension is not base level.
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWRA;<Entity Currency>;A1399;[ICP None];[None];[None];[None];[None];44984167.990000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A1399
2020-07-03 15:21:58,962 ERROR [AIF]:
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 15, Error: The member of the Account dimension is not base level.
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A2090;[ICP None];[None];[None];[None];[None];0.270000000000
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A2090
错误:
Line: 6, Error: No such member | >>>>>>401700 | Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
下面是我的代码,分几行写的:
def main():
fo = open("C:\Users\yannis\py_script\FMC_1198.log", "r", encoding="ISO-8859-1")
ofile = open("C:\Users\yannis\py_script\out_log.txt",'a', newline='')
vinter = ""
vline = ""
vmember = ""
f1 = fo.readlines()
for x in f1:
res = x.partition('Line:')[2]
if len(res) > 0:
vline = res
continue
res = x.partition('>>>>>>')[2]
if len(res) > 0:
vmember = res
continue
res = x.partition('Actual;')[2]
if len(res) > 0:
vinter = res
continue
linha = vinter + vline + vmember
if len(linha) > 0:
print(linha)
ofile.write(linha)
continue
fo.close()
ofile.close()
非常感谢任何帮助!
出现这种情况是因为字符串末尾有\n
,你应该写一个辅助函数
def trimNewlines(st):
return st.replace("\n")
或者
def trimNewlines(st):
return st.rstrip("\n")
如果您只想删除字符串右侧的换行符而不是中间或左侧的换行符
我在想类似下面的事情:
new_log = str()
with open("old_log.txt", 'r') as input_file:
lines = [line[37:-1] for line in input_file.readlines()]
lines = [l for (i,l) in enumerate(lines) if i%4!=3]
remastered = list()
i = 0
while i + 2 < len(lines):
remastered.append(f"{lines[i]} | {lines[i+2]} | {lines[i+1]}")
i += 3
new_log = '\n'.join(remastered)
with open("new_log.txt", 'a') as input_file:
input_file.write(new_log+'\n')
试试这个,使用 zip
+ list splicing
# pre-processing, to remove un-wanted lines & starting character's
lines = []
for l in fo.readlines():
_, y = l.split("[AIF]:")
if y.strip():
lines.append(y)
with open("test.txt", 'w') as f:
# list splicing with zip, job done..
lines = [f"{a} | {b} | {c}\n"
for a, b, c in zip(lines[0::3], lines[2::3], lines[1::3])]
f.writelines(lines)