Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_Loops_Output_Multiple Columns - Fatal编程技术网

python中的列化循环输出

python中的列化循环输出,python,loops,output,multiple-columns,Python,Loops,Output,Multiple Columns,我有一个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

我有一个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
的其他解决方案也受欢迎。

好吧,由于您愿意使用不使用
rjust
ljust
的解决方案,您可以决定要打印多少
并从中减去
行的
len

简短示例:

print(' [+] {0} {1} {2}'.format(line, '.' * (50 - len(line)), out))

好的,由于您对不使用
rjust
ljust
的解决方案持开放态度,因此您可以决定要打印多少
,并从中减去
行的
len

简短示例:

print(' [+] {0} {1} {2}'.format(line, '.' * (50 - len(line)), out))

您可以预先计算最长的URL,并用点左对齐该条目,以便它更完美地配对:

>>> signs = ['-', '+', '-']  
... URLs = ['http://foobar.com', 'http://much_longer_foo.com', 'http://medium_foo.com']  
... tails = ['Longer shouting ending!!', 'different ending', 'Longer shouting ending!!'] 
...   
... maxlen = max(map(len, URLs))  
... linefmt = " [{{sign}}] {{URL:.<{maxlen}}}{{tail:.>30}}".format(maxlen=maxlen)  
... for sign,URL,tail in zip(signs, URLs, tails):  
...     print(linefmt.format(sign=sign, URL=URL, tail=tail)) 
...                                                                                                                                                                                                                                           
 [-] http://foobar.com...............Longer shouting ending!!
 [+] http://much_longer_foo.com..............different ending
 [-] http://medium_foo.com...........Longer shouting ending!!

另外,正如您所看到的,我建议使用一个格式字符串,并分别将符号/URL/结尾传递给它。这将帮助您减少代码重复并简化可维护性。

您可以预先计算最长的URL,并用点对该条目进行左对齐,以便它更完美地配对:

>>> signs = ['-', '+', '-']  
... URLs = ['http://foobar.com', 'http://much_longer_foo.com', 'http://medium_foo.com']  
... tails = ['Longer shouting ending!!', 'different ending', 'Longer shouting ending!!'] 
...   
... maxlen = max(map(len, URLs))  
... linefmt = " [{{sign}}] {{URL:.<{maxlen}}}{{tail:.>30}}".format(maxlen=maxlen)  
... for sign,URL,tail in zip(signs, URLs, tails):  
...     print(linefmt.format(sign=sign, URL=URL, tail=tail)) 
...                                                                                                                                                                                                                                           
 [-] http://foobar.com...............Longer shouting ending!!
 [+] http://much_longer_foo.com..............different ending
 [-] http://medium_foo.com...........Longer shouting ending!!

另外,正如您所看到的,我建议使用一个格式字符串,并分别将符号/URL/结尾传递给它。这将有助于减少代码重复并简化可维护性。

%20s
替换为
%30s
,使其与
rjust
@feliks No匹配,此列表可能包含在其他网站中。@feliks,我用
%30s
更新了输出替换
%20s
,使其与
rjust
@feliks No匹配,此列表可能包含在其他网站中。@feliks,我更新了输出