0

I want to parse a text file using python. I am using split command, to split the line at :,=. I am able to parse it successfully but I want the output as below.

Input:

customer1:124  
clientID:1   
Error=1  
customer2:125  
clientID:1   
Error=2  
customer3:126  
clientID:1  
Error=3 

Desired output:

Customer | Error  
customer1   1  
customer2   2  
customer3   3  

Here is my code:

import re

with open('logfile','r') as log_data:
    for line in log_data:
        if line.startswith("customer") | line.startswith("Error"):
            res = re.split(':|=',line)
            print(res)

Output which I am getting is

['customer1', '123\n']
['Error', '1\n']
['customer1', '124\n']
['Error', '2\n']
['customer1', '125\n']
['Error', '3\n']
['customer1', '126\n']
['Error', '4\n']
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
D Rao
  • 3
  • 2
  • 2
    You should use `or` for logical-or; `|` is bitwise-or. – Karl Knechtel May 18 '20 at 14:12
  • @KarlKnechtel Looks like you did not understand my question, I am able to split the input successfully. The problem what I have is with only printing the output. – D Rao May 18 '20 at 17:52
  • Yes; and by concatenating lists into strings, as described in the duplicate that I linked for you, you will produce the output you need to print. – Karl Knechtel May 19 '20 at 06:23

0 Answers0