删除最后打印的字符python [英] Delete last printed character python
问题描述
我正在用 Python 编写程序,想用另一个字符替换终端中打印的最后一个字符.
I am writing a program in Python and want to replace the last character printed in the terminal with another character.
伪代码为:
print "Ofen",
print "\b", # NOT NECCESARILY \b, BUT the wanted print statement that will erase the last character printed
print "r"
我使用的是 Windows8 操作系统、Python 2.7 和常规解释器.
I'm using Windows8 OS, Python 2.7, and the regular interpreter.
到目前为止我看到的所有选项都不适合我.(如:\010
、'\033[#D'
(#为1)、'\r'
).
All of the options I saw so far didn't work for me. (such as: \010
, '\033[#D'
(# is 1), '\r'
).
这些选项是在其他 Stack Overflow 问题或其他资源中提出的,但似乎对我不起作用.
These options were suggested in other Stack Overflow questions or other resources and don't seem to work for me.
EDIT:同样使用 sys.stdout.write
不会改变影响.它只是不会擦除最后一个打印的字符.相反,当使用 sys.stdout.write
时,我的输出是:
EDIT: also using sys.stdout.write
doesn't change the affect. It just doesn't erase the last printed character. Instead, when using sys.stdout.write
, my output is:
Ofenr # with a square before 'r'
我的问题:
- 为什么这些选项不起作用?
- 如何实现所需的输出?
- 这是否与 Windows 操作系统或 Python 2.7 有关?
- 当我找到怎么做时,是否可以手动擦除(使用想要的橡皮擦),删除在python的
print
中打印的'\n'
声明?
- Why don't these options work?
- How do I achieve the desired output?
- Is this related to Windows OS or Python 2.7?
- When I find how to do it, is it possible to erase manually (using the wanted eraser), delete the
'\n'
that is printed in python'sprint
statement?
推荐答案
在 python 中使用 print
时,会添加换行符(又名 '\n'
).您应该使用 sys.stdout.write()
代替.
When using print
in python a line feed (aka '\n'
) is added. You should use sys.stdout.write()
instead.
import sys
sys.stdout.write("Ofen")
sys.stdout.write("\b")
sys.stdout.write("r")
sys.stdout.flush()
输出:Offer
这篇关于删除最后打印的字符python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!