I need to concatenate three different strings from different lines into one line. My log file looks like this:
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
Error:
Line: 6, Error: No such member | >>>>>>401700 | Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
Below is my code, which is writing in different lines:
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()
Any help much appreciated!
Advertisement
Answer
This happens because there are n
at the end of the strings, you should write a helper function
def trimNewlines(st): return st.replace("n")
Or
def trimNewlines(st): return st.rstrip("n")
If you only want to remove newlines on the right of the strings and not from the middle or left