Paul
Paul

Reputation: 5984

printing table from list of data

I have some data in a list of lists. I'm trying to print it out evenly in a table with format, however diferent lengths really throw it off. Is there any way to do this simply? Or do I have to do something awkward like calculate the maximum of each column and pad the other ones with spaces?

 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  

Upvotes: 0

Views: 141

Answers (5)

dansalmo
dansalmo

Reputation: 11704

A variation that formats the header as well:

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]))

Upvotes: 1

Sylvain Leroux
Sylvain Leroux

Reputation: 52070

Slight variation:

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
          ))

More or less readable depending on you taste:

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

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

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) 

Output:

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

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45670

Just fiddeling with offsets give me this:

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

Output:

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

Upvotes: 2

Sildar
Sildar

Reputation: 302

I think using tabulations ('\t') should do the trick.

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

I tested it via the terminal and it seems to work, aligning it right.

Upvotes: 1

Related Questions