^ H ^? in python

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

doing it

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

I accept the first option and

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

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

 raw_input("userinput>>") 

to capture the entrance.

The only way I could understand was to implement my own shell, which doesn’t work on the “source source”, but on a “char basis”.

Any better (and faster) idea?

+4
source share
3 answers

The built-in raw_input() (or input() function in Python 3) will automatically use the readline library after importing it. This gives you a nice and full-featured line editor, and this is probably your best choice on platforms where it is available if you don’t against Readline, a contagious license (GPL).

+4
source

I definitely don’t know your question. IMO, you need a method to read text based on a string (including some special character) from the console to the program.

No matter what method you use, if this symbol has special meaning in different consoles, you should encounter a console (not only for a specific system, but also for a specific console), all text in the console will be stored in the buffer first, and then show on the screen, finally process and send to your program. Another way around this problem is to use the environment to create the source string.

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

After resolving this issue

using this fragment can work with input:

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

To be faster, perhaps to call some kind of system function, depending on the OS, is an idea. But in fact, IO in python is faster for regular tasks.

0
source

To fix ^? on erase do stty erase ^H

0
source

Source: https://habr.com/ru/post/1392522/


All Articles