我可以使用什么来在Python的终端中进行一行中断?

e-s*_*tis 8 python line-breaks

我可以回来使用\b:

>>> print("123#456")
123#456
>>> print("123#\b456")
123456
Run Code Online (Sandbox Code Playgroud)

但如果涉及换行符,则无效:

>>> print("123#\n456")
123#
456
>>> print("123#\n\b456")
123#
456
Run Code Online (Sandbox Code Playgroud)

有没有办法去破线?

我问这个是因为我在前一行有进展:

53%

我用它\b来更新价值.但是如果有人打印出来的东西,就会破坏它.我试图创建一个字符串缓冲区并打印'\ b'来补偿它,然后打印缓冲区.但如果有换行符,它就不起作用.

Sti*_*anE 11

一种可能的(有点hacky)解决方案是使用'\ 033 [1A'返回一行.将1替换为行数以跳回.您可以使用其他几个转义序列来操作游标.查看完整列表:http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

- Position the Cursor:
  \033[<L>;<C>H
     Or
  \033[<L>;<C>f
  puts the cursor at line L and column C.
- Move the cursor up N lines:
  \033[<N>A
- Move the cursor down N lines:
  \033[<N>B
- Move the cursor forward N columns:
  \033[<N>C
- Move the cursor backward N columns:
  \033[<N>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
Run Code Online (Sandbox Code Playgroud)

请注意,这可能不适用于所有终端.