我想在python中创建一个非常简单的程序,它需要从用户读取输入并相应地写入输出。我需要类似这样的输出:在python中分开读取和写入
$./program.py
say something: Hello World
result: hello world
的事情是,我需要无限期地读取输入,每次用户输入数据我想使印刷数据不会妨碍输入提示。更好的是不打印新行,保留上面的输出:一行阅读,另一行写作。
我尝试过使用curses,但我不想要使用孔屏幕,只是两条线。
我想在python中创建一个非常简单的程序,它需要从用户读取输入并相应地写入输出。我需要类似这样的输出:在python中分开读取和写入
$./program.py
say something: Hello World
result: hello world
的事情是,我需要无限期地读取输入,每次用户输入数据我想使印刷数据不会妨碍输入提示。更好的是不打印新行,保留上面的输出:一行阅读,另一行写作。
我尝试过使用curses,但我不想要使用孔屏幕,只是两条线。
我相信这是你想要什么:
import colorama
colorama.init()
no = 0
while True:
user_input = str(raw_input('\033[2A'*no + '\033[KSay something: '))
print '\033[KResult: ' + user_input
no = 1
这怎么看起来输入字符串后:
这个实现在Windows上工作,但是,如果你使用Linux,如果我不是这些误认为是没有必要的:
import colorama
colorama.init()
编辑:修改我的代码位,所以它不会覆盖了代码执行之前打印的文本。还添加了工作实施的图像。
你可以做veeeery简单的一招:
from os import system
while True:
system('clear') # or 'cls' if you are running windows
user_input = input('say something:')
print('result: ' + user_input)
input()
每次在第一行打印一个字符时,它应该在第二行中回显? – Gabriel