我希望有更好的方法来做到这一点。直接上代码:
print "-I- %-6s%-6s%-6s%-6s%-6s%-6s%-6s%-6s%-8s" % \
("A",B","C","D","E","F","G","H","% Done")
print "-I- %-6s%-6s%-6s%-6s%-6s%-6s%-6s%-6s%-8s" % \
("-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5,"-"*5,"-"*8)
理想情况下,我想做这样的事情:
hdrs = ["A",B","C","D","E","F","G","H","% Done"]
<print statement that uses len(hdrs[i]+2) for the column width>
<print statement that uses len(hdrs[i]+2) for the column width and len(hdrs[i]+1 for the number of dashes>
输出将如下所示:
A B C
----- ----- -----
这种方法将比我目前的方法更具可扩展性。我使用 join 和 map 尝试了各种方法,但我一直无法找到可行的解决方案。任何帮助将不胜感激。
编辑:
我刚刚让这部分工作:
print " ".join("-"*(len(x)+1) for x in hdrs)
上一行代码按照我在原始帖子中要求的方式打印破折号,但我想知道是否有更清洁的方式。我仍然不知道如何打印字符串。