0

我在列表列表中有一些数据。我试图将它均匀地打印在一个有格式的表格中,但是不同的长度真的把它扔掉了。有没有办法简单地做到这一点?还是我必须做一些尴尬的事情,比如计算每列的最大值并用空格填充其他列?

 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  
4

5 回答 5

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
于 2013-06-14T14:41:47.607 回答
1

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

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

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

于 2013-06-14T14:33:37.497 回答
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
于 2013-06-14T15:34:20.517 回答
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]))
于 2013-06-14T16:27:58.433 回答
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
于 2013-06-14T15:54:02.473 回答