将变量中的值打印为三列

时间:2015-02-24 21:16:04

标签: python format screen

我有三个包含数据的变量,我想将它打印成三列。目前数据如下: [       VLAN0010',' VLAN0020',' VLAN0022',' VLAN0025',' VLAN0026',' VLAN0027', ' VLAN0029',' VLAN0031',' VLAN0032',' VLAN0036',' VLAN0037',' VLAN0039& #39;,' VLAN0040',' VLAN0042',' VLAN0043',' VLAN0046',' VLAN0047',& #39; VLAN0050',' VLAN0051',' VLAN0055',' VLAN0056',' VLAN0058',' VLAN0059&# 39;,' VLAN0060',' VLAN0064',' VLAN0066',' VLAN0068',' VLAN0072',&# 39; VLAN0074',' VLAN0075',' VLAN0077',' VLAN0080',' VLAN0090',' VLAN0100&#39 ;,' VLAN0101',' VLAN0102',' VLAN0103',' VLAN0104',' VLAN0105',&#39 ; VLAN0106',' VLAN0107',' VLAN0108',' VLAN0109',' VLAN0110',' VLAN0111' , ' VLAN0112',' VLAN0116',' VLAN0117',' VLAN0118',' VLAN0143',' VLAN0170& #39;,' VLAN0198',' VLAN0201',' VLAN0202',' VLAN0203',' VLAN0204',& #39; VLAN0205',' VLAN0206',' VLAN0207',' VLAN0210',' VLAN0299',' VLAN0801&# 39;,' VLAN0802',' VLAN0803',' VLAN0899',' VLAN0900',' VLAN0901',&# 39; VLAN0902',' VLAN0903',' VLAN0904',' VLAN0999'] ['千兆以太网9/11 \ r','千兆以太网9/11 \ r','千兆以太网9/11 \ r','千兆以太网9/11 \ r&# 39;,'千兆以太网9/15 \ r','千兆以太网9/15 \ r','端口-channel1 \ r','千兆以太网9/19 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' GigabitEthernet12 / 1 \ r',' Port- channel1 \ r',' Port-channel1 \ r',' Port-channel16 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' GigabitEthernet9 / 29 \ r',' GigabitEthernet9 / 29 \ r',' GigabitEthernet9 / 29 \ r',&# 39; GigabitEthernet9 / 10 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' Port-channel16 \ r', ' Port-channel1 \ r',' GigabitEthernet9 / 23 \ r',' GigabitEthernet9 / 23 \ r',' GigabitEthernet12 / 1 \ r&#39 ;,' Port-channel1 \ r',' Port-channel1 \ r',' Port-channel16 \ r',' Port-channel14 \ r && #39;,' Port-channel1 \ r',' Port-channel16 \ r',' Port-cha nnel3 \ r',' Port-channel14 \ r',' Port-channel14 \ r',' Port-channel1 \ r',' Port-channel14 \ r',' Port-channel1 \ r',' GigabitEthernet9 / 17 \ r',' GigabitEthernet9 / 2 \ r',&# 39; GigabitEthernet9 / 3 \ r',' Port-channel1 \ r',' GigabitEthernet9 / 6 \ r',' Port-channel1 \ r', ' GigabitEthernet9 / 8 \ r','千兆以太网9/10 \ r','千兆以太网9/13 \ r','千兆以太网9/29 \ r&#39 ;,'千兆以太网9/15 \ r','千兆以太网9/16 \ r','千兆以太网9/27 \ r','千兆以太网9/17 \ r& #39;,' GigabitEthernet9 / 29 \ r',' Port-channel1 \ r',' Port-channel16 \ r',' Port-channel1 \ r','千兆以太网9/17 \ r','千兆以太网9/18 \ r','千兆以太网9/19 \ r','千兆以太网9 / 24 \ r',' Port-channel1 \ r',' GigabitEthernet9 / 23 \ r',' Port-channel1 \ r',' ; Port-channel1 \ r',' Port-channel1 \ r',' Port-chan nel1 \ r',' Port-channel1 \ r',' GigabitEthernet9 / 29 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' Port-channel1 \ r',' Port-channel1 \ r',&# 39; Port-channel1 \ r',' Port-channel1 \ r'] [' 1d04h',' 1d04h',' 1d04h',' 1d04h',' 1w0d',' 1w0d',' 5w5d',' 3w3d',' 12w6d',' 4w3d',' 1w0d',' 30w3d',' 12w6d',' 17w3d',' 30w3d',' 30w3d',' 3w6d',' 3w6d',' 2w6d',' 4d00h',' 12w6d',' 12w6d',' 17w3d',' 12w6d',' 4d01h',' 4d01h',' 1w0d',' 30w3d',' 2w0d',' 17w3d',' 12w6d']

我希望它能像这样出现: (' VLAN0001','千兆以太网9/11 \ r',' 1d04h')

我是用itertools.izip做的,但在大约20个条目之后它最大化并且不显示所有数据。关于我如何解决这个问题的任何建议?

1 个答案:

答案 0 :(得分:0)

尝试内置zip。如果您的列表是ABC,那么:

rows = zip(A, B, C)

有了这个,您可以使用for循环打印:

for row in rows:
    print row  # ('VLAN0001', 'GigabitEthernet9/11\r', ' 1d04h')
    # or you can format somehow:
    "%s %s %s" % row  # Change the formatting codes as you please
    "{0:s} {1:s} {2:s}".format(*row)  # or use new-style formatting