I need to concatenate three different strings from different lines into one line. My log file looks like this:
JavaScript
x
12
12
1
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 6, Error: No such member
2
2020-07-03 15:21:58,962 ERROR [AIF]: Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
3
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>401700
4
2020-07-03 15:21:58,962 ERROR [AIF]:
5
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 7, Error: The member of the Account dimension is not base level.
6
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
7
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A1399
8
2020-07-03 15:21:58,962 ERROR [AIF]:
9
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 15, Error: The member of the Account dimension is not base level.
10
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
11
2020-07-03 15:21:58,962 ERROR [AIF]: >>>>>>A2090
12
Error:
JavaScript
1
2
1
Line: 6, Error: No such member | >>>>>>401700 | Actual;2020;Jun;YTD;EWHG;<Entity Currency>;A1399;401700;[None];[None];[None];[None];-7537030.790000000000
2
Below is my code, which is writing in different lines:
JavaScript
1
34
34
1
def main():
2
fo = open("C:\Users\yannis\py_script\FMC_1198.log", "r", encoding="ISO-8859-1")
3
ofile = open("C:\Users\yannis\py_script\out_log.txt",'a', newline='')
4
5
vinter = ""
6
vline = ""
7
vmember = ""
8
9
f1 = fo.readlines()
10
for x in f1:
11
12
res = x.partition('Line:')[2]
13
if len(res) > 0:
14
vline = res
15
continue
16
res = x.partition('>>>>>>')[2]
17
if len(res) > 0:
18
vmember = res
19
continue
20
res = x.partition('Actual;')[2]
21
if len(res) > 0:
22
vinter = res
23
continue
24
25
linha = vinter + vline + vmember
26
if len(linha) > 0:
27
print(linha)
28
ofile.write(linha)
29
continue
30
31
32
fo.close()
33
ofile.close()
34
Any help much appreciated!
Advertisement
Answer
This happens because there are n
at the end of the strings, you should write a helper function
JavaScript
1
3
1
def trimNewlines(st):
2
return st.replace("n")
3
Or
JavaScript
1
3
1
def trimNewlines(st):
2
return st.rstrip("n")
3
If you only want to remove newlines on the right of the strings and not from the middle or left