从数据列表中打印表

时间:2013-06-14 14:30:28

标签: python list

我在列表列表中有一些数据。我试图在格式的表格中均匀地打印出来,但不同的长度真的会把它扔掉。有什么方法可以做到这一点吗?或者我是否必须做一些尴尬的事情,比如计算每列的最大值并用空格填充其他列?

 table_data = [['vlan1', '0013.F200.0058', '192.168.2.2'],
               ['vlan20', '0013.F200.0058', '192.168.30.2'],
               ['vlan20', '0010.600a.7026', '192.168.30.4'], 
               ['vlan20', '70ca.9b99.6a82', '192.168.30.1'],
               ['vlan100', '0013.F200.0058', '192.168.110.2']]   


for i in table_data:
    interface,mac,ip = i
    print "Internet  {} {:>18s} {:>7s} {:>8s}".format(ip, mac,'ARPA' ,interface)    


Protocol  Address             Hardware Addr   Type   Interface                  
Internet  192.168.2.2     0013.F200.0058    ARPA     vlan1                       
Internet  192.168.30.2     0013.F200.0058    ARPA    vlan20                      
Internet  192.168.30.4     0010.600a.7026    ARPA    vlan20                      
Internet  192.168.30.1     70ca.9b99.6a82    ARPA    vlan20                      
Internet  192.168.110.2     0013.F200.0058    ARPA   vlan100  

5 个答案:

答案 0 :(得分:2)

只是摆弄偏移给我这个:

print "Internet {:>16}{:>16}{:>8}{:>10}".format(ip, mac, 'ARPA' ,interface)

输出:

Internet      192.168.2.2  0013.F200.0058    ARPA     vlan1
Internet    192.168.110.2  0013.F200.0058    ARPA   vlan100
Internet     192.168.30.2  0013.F200.0058    ARPA    vlan50
Internet     192.168.30.4  0010.600a.7026    ARPA    vlan20
Internet     192.168.30.1  70ca.9b99.6a82    ARPA     vlan2

答案 1 :(得分:1)

我认为使用制表('\ t')应该可以解决问题。

print "Internet\t{}\t{:>18s}\t{:>7s}\t{:>8s}".format(ip, mac,'ARPA' ,interface)

我通过终端对它进行了测试,它似乎正常工作,正确对齐。

答案 2 :(得分:1)

table_data = [['vlan1',   '0013.F200.0058', '192.168.2.2'],
               ['vlan20', '0013.F200.0058', '192.168.30.2'],
               ['vlan20', '0010.600a.7026', '192.168.30.4'], 
               ['vlan20', '70ca.9b99.6a82', '192.168.30.1'],
               ['vlan100','0013.F200.0058', '192.168.110.2']]   

print "Protocol  Address          Hardware Addr   Type   Interface  "
for i in table_data:
    interface,mac,ip = i
    print "Internet  {:15} {:>15} {:^7s} {}".format(ip, mac,'ARPA' ,interface) 

<强>输出:

Protocol  Address          Hardware Addr   Type   Interface  
Internet  192.168.2.2      0013.F200.0058  ARPA   vlan1
Internet  192.168.30.2     0013.F200.0058  ARPA   vlan20
Internet  192.168.30.4     0010.600a.7026  ARPA   vlan20
Internet  192.168.30.1     70ca.9b99.6a82  ARPA   vlan20
Internet  192.168.110.2    0013.F200.0058  ARPA   vlan100

答案 3 :(得分:1)

也可以格式化标题的变体:

header = [['Protocol', 'Address', 'Hardware Addr', 'Type', 'Interface']]
table_data = [['vlan1', '0013.F200.0058', '192.168.2.2'],
              ['vlan20', '0013.F200.0058', '192.168.30.2'],
              ['vlan20', '0010.600a.7026', '192.168.30.4'], 
              ['vlan20', '70ca.9b99.6a82', '192.168.30.1'],
              ['vlan100','0013.F200.0058', '192.168.110.2']]
for i in header + table_data:
print "{}  {:15} {:<14} {:^7s} {}".format(
    *i if i[3:] else ('Internet', i[2], i[1], 'ARPA', i[0]))

答案 4 :(得分:0)

轻微变化:

for i in table_data:
    interface,mac,ip = i

    ip_aligned = "{:>3s}.{:>3s}.{:>3s}.{:>3s}".format(*ip.split('.'))
    mac_normalized = mac.lower() # some MAC addr are lower case other upper case

    print("Internet  {:16s} {:>18s} {:>7s} {:>8s}".format(
                      ip_aligned,
                      mac_normalized,
                      'ARPA'
                      ,interface
          ))

根据您的口味或多或少可读:

Internet  192.168.  2.  2      0013.f200.0058    ARPA    vlan1
Internet  192.168. 30.  2      0013.f200.0058    ARPA   vlan20
Internet  192.168. 30.  4      0010.600a.7026    ARPA   vlan20
Internet  192.168. 30.  1      70ca.9b99.6a82    ARPA   vlan20
Internet  192.168.110.  2      0013.f200.0058    ARPA  vlan100