我有这个代码:
sys.stdout.write("\r [*] Serching for "+FirstName+" AND "+LastName )
sys.stdout.flush()
但是当我把它放在循环中时,我一步一步地将FirstNames彼此混合在一起,并将LastNames彼此混合在一起.
Searching for TEST_THREE AND EXAMPLE_THREE #First time
Searching for TEST_TWOEE AND EXAMPLE_TWOEE #Next time
在TWO之后你看到有三个EE …
我该如何解决?
解决方法:
用额外的空格填充字符串.例如,使用str.ljust
:
msg = "[*] Serching for {} AND {}".format(first_name, last_name)
sys.stdout.write("\r " + msg.ljust(70))
sys.stdout.flush()
使用str.format
:
msg = "[*] Serching for {} AND {}".format(first_name, last_name)
sys.stdout.write("\r {:<70}".format(msg))
sys.stdout.flush()