Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
格式化Python输出_Python_Templates - Fatal编程技术网

格式化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