2014-04-27 52 views
0
a = [('sample\\assignments.html', 'C:\\Users\\AJ\\Desktop\\sample\\sample\\projects.html', 'sample\\projects.html', False),('sample\\assignments.html','C:\\Users\\AJ\\Desktop\\sample\\sample\\docs-assets\\ico\\favicon.png', 'sample\\docs-assets\\ico\\favicon.png', False)] 
print("Broken link") 
print("{:>0}{:>35}".format("Source HTML","Link")) 
print("============================================================") 
for item in a: 
    b = list(item) 
    print("{:>0}{:>35}".format(b[0],b[2])) 

打印结果的格式:印刷用绳子

Broken link 
Source HTML       Link 
============================================================ 
sample\assignments.html    sample\projects.html 
sample\assignments.html sample\docs-assets\ico\favicon.png 
sample\index.html  sample\dist\css\bootstrap.css 
sample\testkeys.html   sample\assignments.html 
sample\testkeys.html     sample\index.html 

有人可以纠正我的代码,这样在第二列“链接”所有文本对齐,或一字排开?

在我的程序中,“a”列表包含许多元组,其中b [0]和b [2]的长度不同。

我真的不确定。{}和{}工作内部的空间指示符如何。

感谢

+0

我想你想要的是allign列。这意味着你应该用whitespaces sush填充所有字符串,它们具有相同的长度,然后在需要时添加'\ t'或更多的空格。我不确定是否有什么已经在Python中做到了。 – displayname

回答

0
print("{:<26}{}".format("Source HTML","Link")) 
print("="*60) 
for item in a: 
    b = list(item) 
    print("{:<26}{}".format(b[0],b[2])) 

输出:

Broken link 
Source HTML    Link 
============================================================ 
sample\assignments.html sample\projects.html 
sample\assignments.html sample\docs-assets\ico\favicon.png