Python3打印功能以对齐正确的对齐列

时间:2018-06-18 15:12:42

标签: python-3.x

我试图将一些原始输出对齐到用python print函数格式化的标题中。

这是来自C程序函数的格式:

QUEUE_NAME     PRIO      STATUS      MAX  JL/U JL/P JL/H NJOBS  PEND  RUN  SUSP
debug           50    Open:Active      -   10    -    -     0     0     0     0
perf13          45    Open:Active      -    -    -    1     0     0     0     0
vntr            45    Open:Active      -    -    -    -     0     0     0     0

下面是我尝试使用python样式打印输出但问题是列数据未正确安装如上所述C程序,例如STATUS列。

QUEUE_NAME    PRIO      STATUS     MAX   JL/U  JL/P  JL/H  NJOBS PEND  RUN  SSUSP
normal        150       -          8     -     -     -     1     -     0    0
lnx64         150       -          2147  -     -     -     0     -     0    0
interactive   50        Open:Active100   50    21    34    0     -     0    0

以下是我尝试的代码,请建议改进​​代码。

$ cat  default.py
def display(data):
    print("\n")
    print('{:<14}{:<10}{:<12}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<5}{:<5}'.format("QUEUE_NAME","PRIO","STATUS","MAX","JL/U","JL/P","JL/H","NJOBS","PEND","RUN","SSUSP"))
    keys = ['queue','priority','status','maxJob','jlu','jlp','jlh','numJobs','pend','numRUN','numSSUP']
    for doc in data['NBS_BATCH_QUEUES']:
        element_lst = []
        for key in keys:
            if key in doc:
                key = doc[key]
            else:
                key = '-'
            element_lst.append(str(key))
        print('{0[0]:14s}{0[1]:10s}{0[2]:12s}{0[3]:6s}{0[4]:6s}{0[5]:6s}{0[6]:6s}{0[7]:6s}{0[8]:6s}{0[9]:5s}{0[10]:5s}'.format(element_lst))

1 个答案:

答案 0 :(得分:0)

我正确地签署了印刷声明,现在看起来都很好,Thnx。

print('{:>14}{:>8}{:>13}{:>8}{:>6}{:>6}{:>6}{:>6}{:>6}{:>8}{:>8}'.format("QUEUE_NAME","PRIO","STATUS","MAX","JL/U","JL/P","JL/H","NJOBS","PEND","RUN","SSUSP"))


print('{0[0]:>14}{0[1]:>8}{0[2]:>13}{0[3]:>8}{0[4]:>6}{0[5]:>6}{0[6]:>6}{0[7]:>6}{0[8]:>6}{0[9]:>8}{0[10]:>8}'.format(element_lst))