如何解决在输入提示中启用箭头键导航
简单的输入循环
while True:
query = input('> ')
results = get_results(query)
print(results)
不允许我使用箭头键
- 在输入的文本中向后移动光标以更改某些内容
- 按下向上箭头以获取过去输入的条目
- 按下箭头可向(2)的相反方向移动
相反,它仅显示所有转义码:
> my query^[[C^[[D^[[D^[[D^[[A^[[A^[[A
如何使其表现得像REPL或Shell提示符?
解决方法
使用cmd
模块创建如下的cmd解释器类。
import cmd
class CmdParse(cmd.Cmd):
prompt = '> '
commands = []
def do_list(self,line):
print(self.commands)
def default(self,line):
print(line[::])
# Write your code here by handling the input entered
self.commands.append(line)
def do_exit(self,line):
return True
if __name__ == '__main__':
CmdParse().cmdloop()
尝试以下一些命令来附加该程序的输出:
mithilesh@mithilesh-desktop:~/playground/on_the_fly$ python cmds.py
> 123
123
> 456
456
> list
['123','456']
> exit
mithilesh@mithilesh-desktop:~/playground/on_the_fly$
有关更多信息,请参见docs
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。