我无法使用事先指定的格式打印我的列表

时间:2016-06-21 00:35:25

标签: python

这就是我要做的事情:

LAYOUT = "{!s:4} {!s:11} {!s:10} {!s:10} {!s:15} {!s:10} {!s:10} {!s:15} {!s:10} {!s:10}"
Item_1 = [002,"Cucharas",12.3,12.5,"4/5/16",200,12.5,"4/6/16",150,140]   
print LAYOUT.format("UPC", "Item", "Cost", "Price", "F_Comp", "QTY", "Profit", "F_Venta", "QTY_Sold", "QTY_Alm")
print LAYOUT.format[Item_1]

我想使用LAYOUT打印多个列表。我实际上从这里的另一个答案中采用了这种格式化方法,但我不断收到以下错误:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print LAYOUT.format[Item_1]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:3)

方括号get_model通常用于索引和切片,它调用[]函数所没有的对象__getitem__方法。像上一行中那样使用括号,并使用str.format解压缩iterable:

*