簡體   English   中英

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 ,您應該編寫一個幫助程序 function

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM