简体   繁体   English

在 Python 输入中使用向上箭头获取先前的输入

[英]Getting Previous Input With Up Arrow in Python Inputs

Problem:问题:

Previously I wrote all of my python code on a Windows 10 machine, but more recently I've been trying to move over to MacOS.以前我在 Windows 10 机器上编写了我所有的 python 代码,但最近我一直在尝试转移到 MacOS。 Everything is working just fine, with one exception.一切正常,只有一个例外。 I'm writing a program that uses the python 'input' function and when pressing on MacOS it prints '^[[a' but on Windows 10 it prints the previous input.我正在编写一个使用 python '输入' function 的程序,当在 MacOS 上按时,它会打印 '^[[a' 但在 Windows 上打印它的前一个输入。 Is there get the previous input with the up arrow on Mac?在 Mac 上是否可以通过向上箭头获取先前的输入?


Example Code:示例代码:

while True:
   string = input()
   print(string)

On Windows hitting will print the previous input but on Mac '^[[a' will print在 Windows 上点击将打印上一个输入,但在 Mac 上 '^[[a' 将打印

Use readline module to provide reading and writing of history files.使用readline模块提供历史文件的读写。 More info on that in the docs文档中的更多信息

import readline

while True:
   string = input()
   print(string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM