格式化Python输出
我使用格式化Python输出,python,templates,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 它没有对齐 为什么会这样?有人能帮我吗?我认为你的模板错了。请看一下这
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