这在 Python '\x1b[2K' 中是什么意思? [英] What does this mean in Python '\x1b[2K'?

查看:87
本文介绍了这在 Python '\x1b[2K' 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解到要清除您在 Python 中打印的一行,请执行以下操作:sys.stdout.write('\x1b[2K')

I've just learnt that to clear a line that you printed in Python, do this: sys.stdout.write('\x1b[2K')

为什么这么复杂?那个奇怪的代码是什么意思?打印命令有什么替代方法吗?

Why is it so complicated? what does that weird code mean? and is there any alternative in print command?

打印确实提供了结束"选项,允许在行中来回移动,但无法清除您打印的内容.通过 \r 覆盖并不总是有效,尤其是当新行比旧行短时.你会得到旧线路的痕迹,所以我需要先清理.谢谢.

Print does offer "end" option that allows to go back and forth in lines, but no way to clear what you printed. Overwriting via \r doesn't always work especially if the new line is shorter than the old one. You will get traces from the old line, so I need clearing first. Thanks.

推荐答案

\x1b[2K 是所谓的 ANSI 终端控制序列.它们是 1970 年代的遗产,今天仍在使用(但已大大扩展)来控制终端仿真器.

\x1b[2K is what's known as an ANSI terminal control sequence. They are a legacy of the 1970s and still used today (but vastly extended) to control terminal emulators.

\x1b 是 ESCAPE 的 ASCII(字面意思是键盘上的 ESC 键).[2K 是擦除当前行"的命令.

\x1b is the ASCII for ESCAPE (literally the ESC key on your keyboard). [2K is the command "erase the current line".

Python 中有许多用于与终端配合使用的库,例如 Urwid.这些库将向您隐藏终端的内部工作,并为您提供更高级别的构造以创建 TUI.

There are many libraries in Python for working with the terminal, such as Urwid. These libraries will hide the inner workings of the terminal from you and give you higher-level constructs to create TUIs.

这篇关于这在 Python '\x1b[2K' 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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