0

Basically, I have a dictionary and I'd like to construct a table from it.

The dictionary is of the form:

dict={
'1':{'fruit':'apple',
     'price':0.60,
     'unit':'pieces',
     'stock':60
},
'2':{'fruit':'cherries',
     'price':15.49,
     'unit':'kg',
     'stock':5.6
},
and so on.
}

I want the table to look like with correct alignment of numbers:

no  |item      | price |   stock
----+----------+-------+----------
1   |apple     |  0.60 | 60 pieces
----+----------+-------+----------
2   |cherries  | 15.49 |  5.6 kg

and so on...

I do NOT want to print this table out, I'm trying to write a function that takes the dict as input and RETURNS this table as a string.

Here's my attempt:

def items(dct)
table="{0:<2} | {1:<33} | {2:^8} | {3:^11}".format("no", "item", "price","stock") 
...
return table

I'm having trouble with formatting strings, I've tried to add line breaks and play around with different things but I always get various errors and things just aren't working out :( I'm new to Python, could someone educate me pls. Thanks!

Neucro
  • 252
  • 2
  • 6
  • _but I always get various errors and things just aren't working out_ Show us the errors, and show us the output you got. – John Gordon Dec 17 '20 at 16:24

3 Answers3

2
def table_create(dct):
    dashes = "{0:<2} + {1:<33} + {2:^8} + {3:^11} \n".format("-"*2, "-"*33, "-"*8, "-"*11)
    table="{0:<2} | {1:<33} | {2:^8} | {3:^11} \n".format("no", "item", "price", "stock")
    table+=dashes
    for key, value in dct.items():
        table+="{0:<2} | {1:<33} | {2:^8} | {3:^11} \n".format(key, value["fruit"], value["price"],str(value["stock"])+" "+value["unit"]) 
        table+=dashes
    return table

print(table_create(dct))

# output
no | item                              |  price   |    stock    
-- + --------------------------------- + -------- + ----------- 
1  | apple                             |   0.6    |  60 pieces  
-- + --------------------------------- + -------- + ----------- 
2  | cherries                          |  15.49   |   5.6 kg    
-- + --------------------------------- + -------- + ----------- 
Harish Vutukuri
  • 1,092
  • 6
  • 14
  • Ahh perfect, I didn't think of breaking the table down into several parts & using for loops. Thank you very much! – Achuan Chen Dec 17 '20 at 16:35
1

the same way you stored the header of the table, you can store its entries and print them or do whatever you want.

    dict={
    '1':{'fruit':'apple','price':0.60,'unit':'pieces','stock':60},
    '2':{'fruit':'cherries','price':15.49,'unit':'kg','stock':5.6}
    }
    
    def items(dct):
        table="{0:<2} | {1:<33} | {2:^8} | {3:^11}".format("no", "item", "price","stock") 
        print(table)
        for i in dict:
            print("{0:<2} | {1:<33} | {2:^8} | {3:^11}".format(i,dict[i]['fruit'] ,dict[i]['price'],str(dict[i]['stock'])+' '+dict[i]['unit']))

items(dict)
Sameer
  • 83
  • 8
1

You can check these questions:

Python - Printing a dictionary as a horizontal table with headers

Printing Lists as Tabular Data

Instead of printing the data, just concatenate it in a string.

Cagri
  • 307
  • 3
  • 13