Prettier output in Python 2.7
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Python format tabular output
如何将以下代码(python 2.7)的输出放到表中?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | with open('blacklists.bls', 'r') as f: L = [dnsbls.strip() for dnsbls in f] t10 = time.time() for listofdnsbls in L: try: t0 = time.time() result = socket.gethostbyname("%s.%s" % (ip_reversed(iinput), listofdnsbls)) t1 = time.time() total = t1-t0 print"%s\t%s\t%s sec" % (listofdnsbls, result, total) except (socket.gaierror): t2 = time.time() error_total = t2-t0 print"%s\tNo result\t%s sec" % (listofdnsbls, error_total) t20 = time.time() totaltotal =t20-t10 print" Completed in: %s sec" % (totaltotal) |
目前的输出不太整齐:
1 2 3 | rbl.ntvinet.net 77.95.250.11 0.00194096565247 sec postfix.bondedsender.org No result 0.329633951187 sec procmail.bondedsender.org No result 6.34444999695 sec |
我希望它更像这样:
1 2 3 | rbl.ntvinet.net 77.95.250.11 0.00194096565247 sec postfix.bondedsender.org No result 0.329633951187 sec procmail.bondedsender.org No result 6.34444999695 sec |
我找到了一些解释如何使用%1d和%2d的文档,但我无法使其生效,因为它们不是数字而是字符串。
也可以在%s修饰符中使用数字。数字的符号决定字符串是左对齐还是右对齐。例如,右对齐字符串的