2017-12-26 12 views
-7

Print Statement ---出力イメージは、私は、Python

二行目の長さで整然とした形式でこれをプリントアウトするにはどうすればよい、それはより多くの文字を持っているとして、私は一定の間隔を維持することができますどのように、私は何かを持って増加していますこのようにforループで印刷することができます。

答えて

0

formatting specification文字列を使用できます。あなたがあなたの状況の完全な例を与えていなかったので、私はあなたのデータの構造を推測する必要が

class Arp_entry: 
    def __init__(self, interface_name, ip_address, mac): 
     self.interface_name = interface_name 
     self.ip_address = ip_address 
     self.mac = mac 

arp_table = [ 
    Arp_entry("em1.0", "128.0.0.16", "00:0c:29:38:fa:60"), 
    Arp_entry("ge-0/0/0.0", "172.17.1.2", "00:0c:29:74:6b:a6"), 
    Arp_entry("ge-0/0/1.0", "172.17.3.2", "00:0c:29:74:6b:b0"), 
    Arp_entry("fxp0.0", "192.168.0.16", "b8:27:eb:45:b8:30"), 
    Arp_entry("fxp0.0", "192.168.0.22", "4c:32:75:96:72:eb"), 
    Arp_entry("fxp0.0", "192.168.0.23", "00:0c:29:af:f0:a5"), 
    Arp_entry("fxp0.0", "192.168.0.24", "00:0c:29:cc:a8:c3") 
    ] 

formatting_string = "{0:<25}{1:<25}{2:<30}" 

print(formatting_string.format("INTF", "IP_ADDRESS", "MAC")) 
print(formatting_string.format("----", "----------", "---")) 

for current_entry in arp_table: 
    print(formatting_string.format(current_entry.interface_name, 
            current_entry.ip_address, 
            current_entry.mac)) 
+0

では、要件がうまく理解している、ありがとう、私はこの試みを与えるだろうあなたに知らせる –