Roberto Passians
Roberto Passians

Reputation: 13

I'm unable to print my list using the format I specified beforehand

This is what I'm trying to do:

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]

I want to print several lists using LAYOUT. I actually took this method of formatting from another answer here, but I keep getting the following error:

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__'

Upvotes: 0

Views: 38

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49318

Square brackets, [], are generally used for indexing and slicing, which calls the object's __getitem__ method, which the str.format function does not have. Use parentheses like you did in the previous line, and unpack the iterable with *:

print LAYOUT.format(*Item_1)

Upvotes: 3

Related Questions