删除最后打印的字符python [英] Delete last printed character python

查看:74
本文介绍了删除最后打印的字符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'

我的问题:

  1. 为什么这些选项不起作用?
  2. 如何实现所需的输出?
  3. 这是否与 Windows 操作系统或 Python 2.7 有关?
  4. 当我找到怎么做时,是否可以手动擦除(使用想要的橡皮擦),删除在python的print中打印的'\n'声明?
  1. Why don't these options work?
  2. How do I achieve the desired output?
  3. Is this related to Windows OS or Python 2.7?
  4. 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's print 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屋!

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