格式 Python 输出

format Python output

我正在使用 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

未对齐 为什么会这样?谁能帮帮我?

我认为您的模板有误。请看这个:

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

在列中提供漂亮的格式:

agasdf                        1      
b                             ddsfg5ggg
hgfdhb                        5ggg