使用宽度说明符打印python列表
我希望有更好的办法。直接进入代码:使用宽度说明符打印python列表,python,list,printing,format,Python,List,Printing,Format,我希望有更好的办法。直接进入代码: 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) 理想情况下,我想做
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
----- ----- -----
format = "".join(["%-"+str(len(h)+2)+"s" for h in hdrs])
这个方法比我当前的方法更具可扩展性。我尝试过使用join和map的各种方法,但是我还没有找到一个可行的解决方案。任何帮助都将不胜感激
编辑:
我刚刚让这部分工作:
print " ".join("-"*(len(x)+1) for x in hdrs)
前一行代码按照我在原始帖子中要求的方式打印破折号,但我想知道是否有更干净的方法。我仍然不知道如何打印字符串。这个怎么样:
hdrs = ("A","B","C","D","E","F","G","H","% Done")
fmt_string = ''.join("%%-%is" % (len(h)+2) for h in hdrs)
print(fmt_string % hdrs)
print(fmt_string % tuple("-"*(len(h)+1) for h in hdrs))
我使用了所描述的列大小,而不是示例中的列大小。这个如何:
hdrs = ("A","B","C","D","E","F","G","H","% Done")
fmt_string = ''.join("%%-%is" % (len(h)+2) for h in hdrs)
print(fmt_string % hdrs)
print(fmt_string % tuple("-"*(len(h)+1) for h in hdrs))
我使用了描述的列大小,而不是示例中的列大小。您可以这样构造格式字符串:
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
----- ----- -----
format = "".join(["%-"+str(len(h)+2)+"s" for h in hdrs])
然后使用它打印列表,例如:
l = range(hdrs) # example data to print, the number of items is the same as hdrs
print format % tuple(l)
您可以这样构造格式字符串:
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
----- ----- -----
format = "".join(["%-"+str(len(h)+2)+"s" for h in hdrs])
然后使用它打印列表,例如:
l = range(hdrs) # example data to print, the number of items is the same as hdrs
print format % tuple(l)
如果你只是想把它做完,而不是把它当作一个练习,那么就使用。此示例来自链接教程:
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
x.align["City name"] = "l" # Left align city names
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print x
输出:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
如果你只是想把它做完,而不是把它当作一个练习,那么就使用。此示例来自链接教程:
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
x.align["City name"] = "l" # Left align city names
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print x
输出:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
试试这个,在这里你可以为每一列指定宽度,不要让字符串大于宽度 宽度=[6,6,6,6,6,6,6,6,8] hdrs=[A、B、C、D、E、F、G、H,完成百分比] 数据=[hdrs[i].ljustwidths[i]用于范围内的i] 宽度=['-'*i表示宽度中的i] 打印“%s”*lendata%tupledata 打印宽度 输出 A B C D E F G H%完成 --- --- --- --- --- --- --- --- ----
试试这个,在这里你可以为每一列指定宽度,不要让字符串大于宽度 宽度=[6,6,6,6,6,6,6,6,8] hdrs=[A、B、C、D、E、F、G、H,完成百分比] 数据=[hdrs[i].ljustwidths[i]用于范围内的i] 宽度=['-'*i表示宽度中的i] 打印“%s”*lendata%tupledata 打印宽度 输出 A B C D E F G H%完成 --- --- --- --- --- --- --- --- ----