^H^?在Python中

发布于 2024-12-28 10:46:26 字数 480 浏览 3 评论 0原文

一些终端将发送 ^? 作为退格键,其他一些终端将发送 ^H。 大多数终端都可以配置来改变它们的行为。 我不想处理所有可能的组合,但我想接受 ^?^H 作为 python 的退格键。

这样做

os.system("stty erase '^?'")

我将接受第一个选项,并且

os.system("stty erase '^H'")

我将接受第二个选项,但第一个选项将不再可用。 我想用它

raw_input("userinput>>")

来获取输入。

我能够弄清楚的唯一方法是实现我自己的 shell,它不适用于“基于原始输入”,而是适用于“基于字符的输入”。

有更好(更快)的想法吗?

Some terminals will send ^? as backspace, some other terminals will send ^H.
Most of the terminals can be configured to change their behavior.
I do not want to deal with all the possible combinations but I would like to accept both ^? and ^H as a backspace from python.

doing this

os.system("stty erase '^?'")

I will accept the first option and with

os.system("stty erase '^H'")

I will accept the second one but the first will be no longer available.
I would like to use

raw_input("userinput>>")

to grab the input.

The only way I was able to figure out is implementing my own shell which works not on "raw based input" but on "char based input".

Any better (and quicker) idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只想待在家 2025-01-04 10:46:26

内置函数 raw_input() (或 Python 3 中的 input())将自动使用 readline 库导入后。这为您提供了一个漂亮且功能齐全的行编辑器,并且只要您不介意 Readline 具有传染性许可证 (GPL),它可能是您在可用的平台上的最佳选择。

The built-in function raw_input() (or input() in Python 3) will automatically use the readline library after importing it. This gives you a nice and full-feautured line editor, and it is probably your best bet on platforms where it is available, as long as you don't mind Readline having a contagious licence (GPL).

末骤雨初歇 2025-01-04 10:46:26

我不太清楚你的问题。 IMO,您需要一种方法来从控制台读取一些基于行的文本(包括一些特殊字符)到程序。

无论你使用什么方法,如果读取这个字符在不同的控制台中有特殊含义,你应该面对一个控制台(不仅是系统特定的,而且是控制台特定的)问题,控制台中的所有文本将首先存储在缓冲区中,并且然后显示在屏幕上,最后处理并发送到您的程序。解决此问题的另一种方法是使用原始行获取控制台环境。

您可以添加特殊方法(装饰器)来装饰 raw_input() 或某种输入方法来处理特殊单词。

使用此代码片段解决该问题后

可以处理输入:

def pre():
    textline=raw_input()
    # ^? should replace to the specific value.
    textline.replace("^?","^H")
    return textline

为了更快,也许调用一些依赖于操作系统的系统功能是一个想法。但事实上,Python 中的 IO 对于常见的工作来说已经足够快了。

I don't know your question exactly. IMO, you need a method to read some line-based text(including some special character) from console to program.

No matter what method you use, if read this character have special mean in different console, you should confront a console(not only system-specific, but also console-specific) question, all text in console will be store in buffer first, and then show in screen, finally processed and send in to your program. Another way to surround this problem is to use a raw line-obtaining console environment.

You can add a special method(a decorator) to decorate the raw_input() or somewhat input method to process special word.

After solved that question

using this snippet can deal with input,:

def pre():
    textline=raw_input()
    # ^? should replace to the specific value.
    textline.replace("^?","^H")
    return textline

To be faster, maybe invoke some system function depend on OS is an idea. But in fact, IO in python is faster enough for common jobs.

狠疯拽 2025-01-04 10:46:26

修复^?擦除时执行stty擦除^H

To fix ^? on erase do stty erase ^H

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文