python运行界面怎么换行,在Python的终端中,我可以使用什么来回行换行?

I can go one caracter back using \b :

>>> print("123#456")

123#456

>>> print("123#\b456")

123456

But it doesn't work if a line break is involved :

>>> print("123#\n456")

123#

456

>>> print("123#\n\b456")

123#

456

Is there a way to go line break back ?

I'm asking this because I have a progress at the previous line:

53%

And I use \b to update the value. But if somebody prints something, it breaks it. I tried to create a buffer of strings and print enought '\b' to compensate for it, then print the buffer back. But it doesn't work if there are line breaks.

解决方案

One possible (a bit hacky) solution is to use '\033[1A' to go back one line. Replace 1 with number of lines to jump back. There are several other escape sequences you can use to manipulate the cursor. Check out the complete list at: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

- Position the Cursor:

\033[;H

Or

\033[;f

puts the cursor at line L and column C.

- Move the cursor up N lines:

\033[A

- Move the cursor down N lines:

\033[B

- Move the cursor forward N columns:

\033[C

- Move the cursor backward N columns:

\033[D

- Clear the screen, move to (0,0):

\033[2J

- Erase to end of line:

\033[K

- Save cursor position:

\033[s

- Restore cursor position:

\033[u

Note that this will probably not work for all terminals.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值