Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split logs into three colums?

I have managed so far to read my Kubernetes pod logs into lines with Python

with open('2215.log','r') as f :
    for line in f.readlines():
             print (line)

Got this output

2023-08-24T12:19:00.536572476+01:00 stderr F Usage:

2023-08-24T12:19:00.536602997+01:00 stderr F    [flags]

2023-08-24T12:19:00.53661012+01:00 stderr F 

2023-08-24T12:19:00.536616965+01:00 stderr F Metrics server flags:

2023-08-24T12:19:00.536623251+01:00 stderr F 

2023-08-24T12:19:00.536631213+01:00 stderr F       --kubeconfig string            The path to the kubeconfig used to connect to the Kubernetes API server and the Kubelets (defaults to in-cluster config)

2023-08-24T12:19:00.536639663+01:00 stderr F       --metric-resolution duration   The resolution at which metrics-server will retain metrics, must set value at least 10s. (default 1m0s)

2023-08-24T12:19:00.536648184+01:00 stderr F       --version                      Show version

2023-08-24T12:19:00.536653981+01:00 stderr F 

2023-08-24T12:19:00.536660756+01:00 stderr F Kubelet client flags:

I want to separate them into

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TIMESTAMP+'stderr F'+ the rest

How to do that?

>Solution :

split takes a second argument to define the maxsplits, i.e.:

with open('2215.log','r') as f :
    for line in f.readlines():
        print(line.split(' ', 2))

Will give you 2 splits (therefore give you 3 parts). Note you have to explicitly define the separator too when using this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading