Search code examples
text-processingpythontext-formattingpython-3.x

Sequentially print data output as a formatted table in Python


I have written a Python script to execute data like

My script :

import os
import os.path
import re
import smtplib
from email.mime.text import MIMEText

infile = r"D:\i2Build\i2SchedulerReport.txt"

if os.path.isfile(infile) and os.access(infile, os.R_OK):
    print "Scheduler report exists and is readable"
else:
    print "Scheduler report is missing or is not readable"

sreport = {}
keep_phrases = ["Scheduler Running is failed"]

with open(infile) as f:
    f = f.readlines()

for line in f:
    for phrase in keep_phrases:
        if phrase in line:
            key,val=line.split(":")
            sreport[key]=val.strip()
            break

for k,v in sreport.items():
    print k,'',v



in2npdvlnx45  => Scheduler Running is failed
bnaxpd01  => Scheduler Running is failed
md1npdaix15  => Scheduler Running is failed
bnaxpd04  => Scheduler Running is failed
bnwspd03  => Scheduler Running is failed
md1npdsun10  => Scheduler Running is failed
bn2kpd14  => Scheduler Running is failed
md1npdvbld02  => Scheduler Running is failed
bnhppd05  => Scheduler Running is failed
dlaxpd02  => Scheduler Running is failed
cmwspd02  => Scheduler Running is failed

I want the above data to execute in tabular format like below and it sends the output table format to mail using MIME import modules or some other. I see that importing pandas is helpful but unable to do it

Expected output:

in2npdvlnx45   Scheduler Running is failed
bnaxpd01       Scheduler Running is failed
md1npdaix15    Scheduler Running is failed
bnaxpd04       Scheduler Running is failed
bnwspd03       Scheduler Running is failed
md1npdsun10    Scheduler Running is failed
bn2kpd14       Scheduler Running is failed
md1npdvbld02   Scheduler Running is failed
bnhppd05       Scheduler Running is failed
dlaxpd02       Scheduler Running is failed
cmwspd02       Scheduler Running is failed

Solution

  • You can use format. Instead of:

    print k,'',v
    

    Use:

    print('{:14} {}'.format(k, v))
    

    More about this here