避免使用Python标准输入流中的控制序列(如^ [[C)) [英] Avoid control sequences(like ^[[C) from Python standard inputstream

查看:54
本文介绍了避免使用Python标准输入流中的控制序列(如^ [[C))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello^[[D
The Entered String is : hello
The Length of Entered String is : 8

当我按下箭头键^ [[C时出现,而不是光标移动(其他箭头键,退出键,home,end也会发生类似的情况!)!

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

这是字符串第二次包含以下字符的情况:

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

因此,'\ x1b','[','C'是从键盘发送到外壳的字符序列,用于表示向右箭头键(光标向前).

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

我想要的是这些字符不会显示在外壳中,但是光标会移动(按所按下的键向前,向后,到原点,结束等).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

输入后进行处理的意义不大,因为主要目的是让光标移动!

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

我如何在Python中实现这一目标?

How can i achieve this in Python?

但是如果我们直接使用python解释器就可以了:

But its ok if we use the python interpreter directly :

这是终端输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5

尽管按了箭头键或进行了逃生或回零操作,输出还是一样.

Here in spite of pressing the arrow keys or escape or home the output is same.

唯一的目的是在使用程序时为用户提供精确的终端体验.

The one and only aim is to give the user a exact terminal like experience while using the program.

推荐答案

input 的文档中提到了以下内容:

The documentation for input mentions the following:

如果readline模块已加载,则input()将使用它提供详细的行编辑和历史记录功能.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

因此,我们采用您的程序,并添加一个 import readline ,现在我们可以从中获取游标处理和其他项目:

So, we take your program, and add an import readline, and now we get cursor handling and other items from it:

import readline

s = None

while s != 'exit':
    s = input("Enter a String : \n")
    print("The Entered String is : '{}'".format(s))
    print("The Length of Entered String is : {}".format(len(s)))

在这种情况下,我可以运行它:

in this case I can do a run of it:

Enter a String :
hello world
The Entered String is : 'hello world'
The Length of Entered String is : 11
Enter a String :
hello   world
The Entered String is : 'hello   world'
The Length of Entered String is : 13
Enter a String :
exit
The Entered String is : 'exit'
The Length of Entered String is : 4

我能够使用向上箭头返回并编辑先前的输入.

I was able to use the up arrow to go back and edit the previous input.

这篇关于避免使用Python标准输入流中的控制序列(如^ [[C))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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