格式化Python输出

时间:2015-02-12 22:46:00

标签: python templates

我正在使用template.format()来对齐两列,例如

a    1
b    3
c    6 

我尝试了以下内容:

template = "{0:30}{2:7}"
w.write("| "+(template.format("a",len(a.findall(info)))+"\n"))
w.write("| "+template.format("b",count)+"\n")

我有这个

a   1
b 5

它没有对齐 为什么会这样?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为你的模板错了。请看一下:

template = "{:30}{:7}"
print(template.format("agasdf","1"))
print(template.format("b","ddsfg5ggg"))
print(template.format("hgfdhb","5ggg"))

在列中提供了很好的格式:

agasdf                        1      
b                             ddsfg5ggg
hgfdhb                        5ggg