Последовательная печать данных в виде отформатированной таблицы в Python - PullRequest
0 голосов
/ 11 октября 2018

Я написал скрипт Python для выполнения таких данных, как

Мой сценарий:

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

Я хочу, чтобы вышеуказанные данные выполнялись в табличном формате, как показано ниже, и он отправляет формат выходной таблицына почту, используя модули импорта MIME или другие.Я вижу, что импорт панд полезен, но не может это сделать

Ожидаемый результат:

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

1 Ответ

0 голосов
/ 11 октября 2018

Вы можете использовать format.Вместо:

print k,'',v

Использование:

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

Подробнее об этом здесь

...