gpt4 book ai didi

python - string.format,不同长度打破表

转载 作者:行者123 更新时间:2023-12-01 05:42:44 25 4
gpt4 key购买 nike

我正在尝试将列表列表格式化为表格。它大部分都有效,但如果一个元素太长,它似乎会破坏表格。知道为什么会发生这种情况,或者如何使用此方法确保此处的表格格式良好?

 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'],
['vlan21', '0013.F200.0058', '192.168.222.222'],
['vlan100', '0013.F200.0058', '192.168.110.2'],
['vlan2100', '0013.F200.0058', '192.168.222.223']]



for i in table_data:
interface,mac,ip = i
print "Internet {}\t {:>18s}\t{:>7s}\t {:<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.222.222 0013.F200.0058 ARPA vlan21
Internet 192.168.110.2 0013.F200.0058 ARPA vlan100
Internet 192.168.222.223 0013.F200.0058 ARPA vlan2100

最佳答案

您可以使用左对齐格式,不是吗?请查看此文档 Format Specification Mini-Language

for i in table_data:
interface,mac,ip = i
print '{:<20s}{:<20s}{:<20s}{s}'.format(ip, mac,'ARPA' ,interface)

关于python - string.format,不同长度打破表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17112207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com