Python 带分拣的台式打印机

Python 带分拣的台式打印机,python,Python,我想创建一个简单的表来打印和排序。这就是我所拥有的: from tabulate import tabulate def main(): # Create a list of strings tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose

我想创建一个简单的表来打印和排序。这就是我所拥有的:

 from tabulate import tabulate
 def main():
 # Create a list of strings
 tableData = [['apples', 'oranges', 'cherries', 'banana'],
            ['Alice', 'Bob', 'Carol', 'David'],
            ['dogs', 'cats', 'moose', 'gooses']]

  # Sort the list
   tableData.sort()

  # Print the table
  print tabulate(tableData)

  # Call the main function.
  main()

我的语法正确吗?有其他方法可以做到这一点吗?

您可能希望能够手动打印表格,而不是使用表格,因为这样可以让您在表格显示方式上有更大的自由度,但这需要付出更多的努力! 我发现python string.format()方法对于这类任务非常有用,下面是我使用.format()编程的表格打印机示例:


如果需要,您可以轻松地调整表格,使其具有列和行线、居中对齐的记录、0填充的数据等-这比制表更自由。

还请注意,有些缩进看起来有点不对劲,但这可能与堆栈溢出的代码块弄乱了缩进有关。
Table=[['Name','Age','Level','Score'],['Tom','15','5','999'],
   ['Bob','105','7','2']]

LongestItem=0
for List in Table:
    for Item in List:
        if len(Item)>LongestItem:
            LongestItem=len(Item)

for List in Table:
    for Item in enumerate(List):
        List[Item[0]] = '{:{}}'.format(List[Item[0]],LongestItem)

for Index in range(len(Table)):
    print(' '.join(Table[Index]))