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!