从字典和字符串格式创建 table - Python
Creating table from dictionary & string formatting - Python
基本上,我有一本字典,我想从中构造一个 table。
字典的格式为:
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.
}
我希望 table 看起来像数字正确对齐:
no |item | price | stock
----+----------+-------+----------
1 |apple | 0.60 | 60 pieces
----+----------+-------+----------
2 |cherries | 15.49 | 5.6 kg
and so on...
我不想打印这个table,我正在尝试编写一个将字典作为输入的函数,RETURNS 这个 table 作为一个字符串。
这是我的尝试:
def items(dct)
table="{0:<2} | {1:<33} | {2:^8} | {3:^11}".format("no", "item", "price","stock")
...
return table
我在格式化字符串时遇到了问题,我尝试添加换行符并尝试不同的东西,但我总是遇到各种错误,而且事情就是没有解决:(
我是 Python 的新手,有人可以教我吗?
谢谢!
与存储 table 的 header 的方式相同,您可以存储其条目并打印它们或做任何您想做的事情。
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)
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
-- + --------------------------------- + -------- + -----------
您可以检查这些问题:
Python - Printing a dictionary as a horizontal table with headers
Printing Lists as Tabular Data
无需打印数据,只需将其连接成一个字符串即可。
基本上,我有一本字典,我想从中构造一个 table。
字典的格式为:
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.
}
我希望 table 看起来像数字正确对齐:
no |item | price | stock
----+----------+-------+----------
1 |apple | 0.60 | 60 pieces
----+----------+-------+----------
2 |cherries | 15.49 | 5.6 kg
and so on...
我不想打印这个table,我正在尝试编写一个将字典作为输入的函数,RETURNS 这个 table 作为一个字符串。
这是我的尝试:
def items(dct)
table="{0:<2} | {1:<33} | {2:^8} | {3:^11}".format("no", "item", "price","stock")
...
return table
我在格式化字符串时遇到了问题,我尝试添加换行符并尝试不同的东西,但我总是遇到各种错误,而且事情就是没有解决:( 我是 Python 的新手,有人可以教我吗? 谢谢!
与存储 table 的 header 的方式相同,您可以存储其条目并打印它们或做任何您想做的事情。
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)
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
-- + --------------------------------- + -------- + -----------
您可以检查这些问题:
Python - Printing a dictionary as a horizontal table with headers
Printing Lists as Tabular Data
无需打印数据,只需将其连接成一个字符串即可。