Python 2.6.6 - 如何以对齐列的表格形式打印列表?

问题描述 投票:0回答:1

用例很简单。我有一个不同长度的字符串列表,我想打印它们,以便它们以表格形式显示(见下文)。考虑列出计算机上的目录。它将它们显示为适合当前窗口大小的表,并考虑列表中的最长值,以便所有列都对齐。根据我的需要,我会在需要换行之前指定最大行长度。

config                 constants.py           fuzzer
fuzzer_session         generator.py           README.docx
run_fuzzer_session.py  run_generator.py       tools
util                   VM_Notes.docx          wire_format_discoverer
xml_processor

我提出了一种方法来做到这一点,但显然它只适用于较新版本的python。它工作的系统使用python 3.6.4。我需要在使用2.6.6版本的系统上执行此操作。当我在这个系统上尝试我的代码时,我收到以下错误:

$ python test.py . 80
Traceback (most recent call last):
  File "test.py", line 46, in <module>
    main()
  File "test.py", line 43, in main
    printTable(dirList, maxLen)
  File "test.py", line 27, in printTable
    printList.append(formatStr.format(item))
ValueError: zero length field name in format

我假设我用来构建格式说明符的技术是它所抱怨的。

这是逻辑:

注意:我通过获取目录列表在此示例中生成我的列表。我的真实用例不使用目录列表。

import os
import sys

def printTable(aList, maxWidth):
    if len(aList) == 0:
        return

    itemMax = 0
    for item in aList:
        if len(item) > itemMax:
            itemMax = len(item)

    if maxWidth > itemMax: 
        numCol = int(maxWidth / itemMax)
    else:
        numCol = 1

    index = 0
    while index < len(aList):
        end = index + numCol
        subList = aList[index:end]
        printList = []

        for item in subList:
            formatStr = '{:%d}' % (itemMax)
            printList.append(formatStr.format(item))

        row = ' '.join(printList)
        print(row)
        index += numCol

def main():
    if len(sys.argv) < 3:
        print("Usage: %s <directory> <max length>" % (sys.argv[0]))
        sys.exit(1)

    aDir = sys.argv[1]
    maxLen = int(sys.argv[2])

    dirList = os.listdir(aDir)
    printTable(dirList, maxLen)

if __name__ == "__main__":
    main()

有没有办法在python 2.6.6上实现我想要做的事情?

我确信有更好的(更“pythonic”)方式来执行这里执行的一些步骤。我做我所知道的。如果有人想提出更好的方法,欢迎您提出意见。

以下是成功运行的示例:

>python test.py .. 80
config                 constants.py           fuzzer
fuzzer_session         generator.py           README.docx
run_fuzzer_session.py  run_generator.py       tools
util                   VM_Notes.docx          wire_format_discoverer
xml_processor

>python test.py .. 60
config                 constants.py
fuzzer                 fuzzer_session
generator.py           README.docx
run_fuzzer_session.py  run_generator.py
tools                  util
VM_Notes.docx          wire_format_discoverer
xml_processor

>python test.py .. 120
config                 constants.py           fuzzer                 fuzzer_session         generator.py
README.docx            run_fuzzer_session.py  run_generator.py       tools                  util
VM_Notes.docx          wire_format_discoverer xml_processor
python python-2.6
1个回答
2
投票
    for item in subList:
        formatStr = '{:%d}' % (itemMax)
        printList.append(formatStr.format(item))

根据ValueError: zero length field name in format in Python2.6.6,你不能在2.6.6中有一个“匿名”格式字符串。像"{:10}"这样的语法直到2.7才成为合法的。在此之前,您需要提供一个显式索引,如"{0:10}"

    for item in subList:
        formatStr = '{0:%d}' % (itemMax)
        printList.append(formatStr.format(item))

...但我觉得你可以通过跳过所有这些并使用ljust来省去一些麻烦。

    for item in subList:
        printList.append(str(item).ljust(itemMax))
© www.soinside.com 2019 - 2024. All rights reserved.