我在python中有这样一个代码:
def sth(mlist):
for line in mlist:
out = func(param1, param2)
if out:
print " [+] %-15s %20s " % (line, out.rjust(30, '.'))
else:
a = 'Not Covered!!'
print " [-] %-15s %20s " % (target, a.rjust(30, '.'))
我在运行代码时得到了糟糕的结果:
[-] http://a.com .................Not Covered!!
[-] http://abcd.info .................Not Covered!!
[+] http://abcdef.net ....................something
[-] https://c.com .................Not Covered!!
[+] https://efghij.org .................other thing
.
.
.
如何获得此类线程的最佳输出形式,例如:
[-] http://a.com ......................... Not Covered!!
[-] http://abcd.info ..................... Not Covered!!
[+] http://abcdef.net .................... something
[-] https://c.com ........................ Not Covered!!
[+] https://efghij.org ................... other thing
.
.
.
甚至其他解决方案,而不是使用ljust
也很受欢迎。你知道吗
您可以预先计算最长的URL,并用点对齐该条目,以便它更完美地配对:
如您所见,我改为使用
.format
而不是百分比格式。这主要是一个偏好的问题。你知道吗重要的是,我们首先用最长URL的长度构建格式字符串,然后使用生成的格式字符串。结果与预期略有不同:圆点周围没有空格。如果您坚持要添加它们,可以手动添加它们。你知道吗
请注意,在Python3.6及更高版本中,可以使用f-strings更优雅地执行此操作:
另外,如您所见,我建议使用一个格式字符串,并分别传递sign/URL/ending。这将帮助您减少代码重复并简化可维护性。你知道吗
好吧,由于您对不使用
rjust
或ljust
的解决方案持开放态度,您可以决定要打印多少.
,并从中减去line
的len
。你知道吗简短示例:
相关问题 更多 >
编程相关推荐