'\b'(退格键)有什么用
What is the use of '\b' (backspace)
我现在正在学习 Python class,我刚刚了解了退格键。和换行符(\n)一样,退格是一个特殊的字符,ASCII码为8。我的老师想不出使用它的理由,但我很好奇它是如何使用的。也许只是历史原因?当我尝试 print("Hellow\b World")
时,我得到了我期望的结果:Hello World
。
退格字符的原因是什么,如何使用?
编辑:我知道它不是 python 具体的,但是在写最初的问题时我主要考虑 Python 而忘记了这个事实。我已尝试编辑以使其更清楚。
这不是Python的特性,而是ASCII定义的符号。 Python 只是支持它(像所有其他语言一样)。
更具体地说,它是一个 control character,用于擦除最后打印的字符或叠印它。 ASCII 的第一个版本于 1963 年发布,当时输出符号的常用方法是将它们发送到打印机并在纸上打印字母。这是维基百科的摘录:
Printing control characters were first used to control the physical mechanism of printers, the earliest output device. [...] The backspace character (BS) moves the printing position one character space backwards. On printers, this is most often used so the printer can overprint characters to make other, not normally available, characters. On terminals and other electronic output devices, there are often software (or hardware) configuration choices which will allow a destruct backspace (i.e., a BS, SP, BS sequence) which erases, or a non-destructive one which does not.
也许首先了解那里发生的事情会有所帮助:
print()
正在写入标准输出,它正在写入所有内容,包括 w
和退格键。
现在必须要显示它。很可能是终端仿真器。
理论上会发生的情况是 w
被显示然后被删除,但它没有被刷新并且它实际发生的速度太快了。
所以现实生活中的应用程序几乎总是在打印文本的开头使用 \b。
我写了一个简短的例子,在进度指示器上有一个小旋转器。示例打印 "-"
后跟 "\b\"
(删除 -
并将其替换为 \
)然后是 "\b|"
(删除 \
并替换它与 |
) 等等。
这样 -
\
|
/
-
\
|
/
看起来像动画旋转线。
#!/usr/bin/env python3
import time
spinner="\|/-"
print ("----------\r", end='', flush=True)
for pos in range(10):
print ("-", end='', flush=True)
for spin in range(25):
#here should be a break as soon as some task proceeded further
time.sleep(0.1)
print ("\b" + spinner[spin % 4], end='', flush=True)
print ("\b*", end='', flush=True)
print ()
P.S.: 许多现有程序使用控制字符,如 \b
\r
3
来显示状态行。最受欢迎的可能是 wget。我也至少通过一个 python 脚本看到过这样的输出(尽管我不记得是哪一个了)
退格键是一个控制字符,可将光标在控制台中向后移动一个字符,但不会将其删除。
What's the reason for the backspace character, and how might it be used?
历史上曾在 ASCII 世界中使用它来打印 accented 个字符。
例如 à 可以使用三个字符序列 a
Backspace
`
生成(或者,使用字符的十六进制值 0x61 0x08 0x60
)。
查看更多退格键here
退格键与退格字符
很多人将两者混淆。键盘上的退格键几乎具有删除前一个字符的通用功能(= 向后移动光标并删除该字符)。但是,退格符 '\b'
只会将光标移回控制台中的一个位置 window 而不会删除它。
它如何工作的一个小例子:
>>> name = "Robert" + "\b\b\b\b" + 'p' + "\b\b" + 'u'
>>> print(text)
Rupert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u']
>>> text += "\bob"
>>> print(text)
Robert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u', '\x08', 'o', 'b']
我现在正在学习 Python class,我刚刚了解了退格键。和换行符(\n)一样,退格是一个特殊的字符,ASCII码为8。我的老师想不出使用它的理由,但我很好奇它是如何使用的。也许只是历史原因?当我尝试 print("Hellow\b World")
时,我得到了我期望的结果:Hello World
。
退格字符的原因是什么,如何使用?
编辑:我知道它不是 python 具体的,但是在写最初的问题时我主要考虑 Python 而忘记了这个事实。我已尝试编辑以使其更清楚。
这不是Python的特性,而是ASCII定义的符号。 Python 只是支持它(像所有其他语言一样)。
更具体地说,它是一个 control character,用于擦除最后打印的字符或叠印它。 ASCII 的第一个版本于 1963 年发布,当时输出符号的常用方法是将它们发送到打印机并在纸上打印字母。这是维基百科的摘录:
Printing control characters were first used to control the physical mechanism of printers, the earliest output device. [...] The backspace character (BS) moves the printing position one character space backwards. On printers, this is most often used so the printer can overprint characters to make other, not normally available, characters. On terminals and other electronic output devices, there are often software (or hardware) configuration choices which will allow a destruct backspace (i.e., a BS, SP, BS sequence) which erases, or a non-destructive one which does not.
也许首先了解那里发生的事情会有所帮助:
print()
正在写入标准输出,它正在写入所有内容,包括 w
和退格键。
现在必须要显示它。很可能是终端仿真器。
理论上会发生的情况是 w
被显示然后被删除,但它没有被刷新并且它实际发生的速度太快了。
所以现实生活中的应用程序几乎总是在打印文本的开头使用 \b。
我写了一个简短的例子,在进度指示器上有一个小旋转器。示例打印 "-"
后跟 "\b\"
(删除 -
并将其替换为 \
)然后是 "\b|"
(删除 \
并替换它与 |
) 等等。
这样 -
\
|
/
-
\
|
/
看起来像动画旋转线。
#!/usr/bin/env python3
import time
spinner="\|/-"
print ("----------\r", end='', flush=True)
for pos in range(10):
print ("-", end='', flush=True)
for spin in range(25):
#here should be a break as soon as some task proceeded further
time.sleep(0.1)
print ("\b" + spinner[spin % 4], end='', flush=True)
print ("\b*", end='', flush=True)
print ()
P.S.: 许多现有程序使用控制字符,如 \b
\r
3
来显示状态行。最受欢迎的可能是 wget。我也至少通过一个 python 脚本看到过这样的输出(尽管我不记得是哪一个了)
退格键是一个控制字符,可将光标在控制台中向后移动一个字符,但不会将其删除。
What's the reason for the backspace character, and how might it be used?
历史上曾在 ASCII 世界中使用它来打印 accented 个字符。
例如 à 可以使用三个字符序列 a
Backspace
`
生成(或者,使用字符的十六进制值 0x61 0x08 0x60
)。
查看更多退格键here
退格键与退格字符
很多人将两者混淆。键盘上的退格键几乎具有删除前一个字符的通用功能(= 向后移动光标并删除该字符)。但是,退格符 '\b'
只会将光标移回控制台中的一个位置 window 而不会删除它。
它如何工作的一个小例子:
>>> name = "Robert" + "\b\b\b\b" + 'p' + "\b\b" + 'u'
>>> print(text)
Rupert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u']
>>> text += "\bob"
>>> print(text)
Robert
>>> print(list(text))
['R', 'o', 'b', 'e', 'r', 't', '\x08', '\x08', '\x08', '\x08', 'p', '\x08', '\x08', 'u', '\x08', 'o', 'b']