gpt4 book ai didi

python - python表格中的适当间距

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:10 25 4
gpt4 key购买 nike

Animal ID Location1 Location2
a01 2 1
a03 4 4
a02 0 3
================================================================================================================

所以这是我使用以下代码得到的表格:

print("Animal ID","Location1","Location2")       
for k, v in d.items():
print(k, end='\t')
print(v.count('s1'), end='\t')
print(v.count('s2'))
print("============================================================================================================")

但是正如人们所看到的那样,表格的间距不正确,我怎样才能修复它以获得这样的表格:

Animal Id Station 1 Station 2 
a01 2 1
a02 0 3
a03 4 4
==============================================

这是任何人都想知道的数据。它的格式是 animal id:data:location

a01:01-24-2011:s1 
a03:01-24-2011:s2
a02:01-24-2011:s2
a03:02-02-2011:s2
a03:03-02-2011:s1
a02:04-19-2011:s2
a01:05-14-2011:s2
a02:06-11-2011:s2
a03:07-12-2011:s1
a01:08-19-2011:s1
a03:09-19-2011:s1
a03:10-19-2011:s2
a03:11-19-2011:s1
a03:12-19-2011:s2

最佳答案

您可以使用 format形成输出字符串

a = [(i, i+1, i+2) for i in range(1, 20, 3)]

print("Animal ID","Location1","Location2")
for x, y, z in a:
s = "{:<9} {:^9} {:^9}".format(x,y,z)
print(s)

关于python - python表格中的适当间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185180/

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