在 Python 中按顺序打印数据输出为格式化的 table
Sequentially print data output as a formatted table in Python
我写了一个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 导入模块或其他方式将输出 table 格式发送到邮件。我看到导入 pandas 很有用,但无法导入
预期输出:
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
您可以使用 format
。而不是:
print k,'',v
使用:
print('{:14} {}'.format(k, v))
有关此内容的更多信息here
我写了一个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 导入模块或其他方式将输出 table 格式发送到邮件。我看到导入 pandas 很有用,但无法导入
预期输出:
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
您可以使用 format
。而不是:
print k,'',v
使用:
print('{:14} {}'.format(k, v))
有关此内容的更多信息here