gpt4 book ai didi

python - 如何在 python 中将内容放入表中

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:54 24 4
gpt4 key购买 nike

如何在 python 中制作表格。我在学校这样做,那里不允许我使用任何额外的插件,例如 tabulate 或 texttable 和漂亮的表格,所以你能指导我如何做到这一点吗?谢谢,它适用于 python 3.4 或 python 3.5

最佳答案

这是基于@SOReadytoHelp 的解决方案。我将其更新为 Python 3 并包含了一个示例。

def print_table(table):
col_width = [max(len(str(x)) for x in col) for col in zip(*table)]
for line in table:
print("| " + " | ".join("{:{}}".format(x, col_width[i])
for i, x in enumerate(line)) + " |")


table = [['/', 2, 3],
['a', '2a', '3a'],
['b', '2b', '3b']]

print_table(table)

打印内容

| / |  2 |  3 |
| a | 2a | 3a |
| b | 2b | 3b |

关于python - 如何在 python 中将内容放入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com