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

2024-09-28 21:27:30 发布

您现在位置: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__'

Tags: format列表itempriceqtyprintlayoutupc
1条回答
网友
1楼 · 发布于 2024-09-28 21:27:30

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

print LAYOUT.format(*Item_1)

相关问题 更多 >