如何从python打印输出中删除字符 [英] how to remove characters from printed output in python

查看:1345
本文介绍了如何从python打印输出中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从其他打印对帐单中删除打印在我的打印对帐单中的多余字符.

I am trying to remove extra characters printed out in my print statement from another print statement.

这是我的代码的样子:

print(addedlist)  #addedlist = [9,5,1989,1,2,3,4,5,6,7,8,9]

for x in range(0, len(addedlist)):
    print('%d->'%addedlist[x],end="")

print('\n')

输出如下:

[9, 5, 1989, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9->5->1989->1->2->3->4->5->6->7->8->9->  

我正在尝试删除最后的->个字符.我试着做:

I am trying to remove the last -> characters. I tried doing :

print(addedlist)  #addedlist = [9,5,1989,1,2,3,4,5,6,7,8,9]

for x in range(0, len(addedlist)):
     print('%d->'%addedlist[x],end="")

print('\b\b\n')

但是没有用.

我将如何实现这一目标?

How would i go about accomplishing this ?

请稍作澄清,我知道我可以更改原始打印语句以更正确地打印它,以避免出现尾随的-> ...一旦出现错误,我正在寻找一种解决方法,以消除尾随的'->'制成

Just some clarification, I know i can change my original print statement to print it more correctly to avoid trailing -> ... I am after a solution for how to erase trailing '->' this once the mistake has been made

推荐答案

我注意到您想使用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()).

这篇关于如何从python打印输出中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆