且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从python打印输出中删除字符

更新时间:2023-12-04 10:30:34

我注意到您想使用carriage return命令.首先,您需要同时学习\b(将有效位置移动到先前位置)和\r(将有效位置移动到行的开头)适用于当前有效行.
我认为您正在使用命令行解释器.您在该处明确按Enter键以进入下一行.
在命令行解释器上,按以下方式使用;:

I notice you wants to play with carriage return commands. First you need to learn both \b (moves the active position to the previous position) and \r (moves the active position to the start of line) works for current active line.
I think you are working on command line interpreter; at which you explicitly press enter for next line.
On command line interpreter use ; as follows:

>>> print("abb", end=""); print("\b\bcc")
acc
>>> print("a->", end=""); print("\b\b  ")
>>> a

如果您正在使用某些脚本,那么您的代码应该可以使用,请参见:

If you are using some script then your code should work, see:

Upload$ cat s.py
print ("abcd->", end="")
print ("\b\b  ")
Upload$ python3.2 s.py
abcd    # notice -> is removed  

但这不是很有用,正确的方法是 Aशwiniचhaudhary 在他的answer .

But this is not much useful, correct approach is what Aशwini चhaudhary has shown in his answer.

我发现您输入的代码有误

I found you mistake in your code

print('%d->' % addedlist[x], end="")    
print('\b\b\n')

您可以使用'\b'将活动位置移动到上一个位置,但不会覆盖"->",您只需输出'\n'将光标移至下一行,则应按以下所示纠正代码./p>

You use '\b' to moves the active position to the previous position, but you don't overwrite "->", you just outputs '\n' that shift cursors to next line, you should rectify your code as below.

print('%d->' % addedlist[x], end="")    
print('\b\b   \n')
#          ^^^  /b then overwrite with spaces 

$ python scriptname.py的身份在Linux shell上运行(对于Python命令行解释器,您可以使用我写的代码,只是播放,使用str.join或使用print()中的sep参数).

Run at Linux shell as $ python scriptname.py (For command line Python's interpreter you can use something like code I written, it is just to play, use str.join or use sep parameter in print()).