简体   繁体   English

Python输入结束而不显示换行符

[英]Python input that ends without showing a newline

Is there an python function similar to raw_input but that doesn't show a newline when you press enter. 是否存在类似于raw_input的python函数,但是当您按Enter键时不会显示换行符。 For example, when you press enter in a Forth prompt it doesn't show a newline. 例如,当您在Forth提示符中按Enter键时,它不会显示换行符。
Edit: 编辑:
If I use the code: 如果我使用代码:

data = raw_input('Prompt: ')
print data

than the output could be: 比输出可能是:

Prompt: Hello
Hello

because it printed a newline when I pressed enter. 因为当我按下回车键时它会打印一个换行符。 I want a function similar to raw_input that doesn't show the newline. 我想要一个类似于raw_input的函数,它不显示换行符。 So if the function I wanted was called special_input and I used the code: 所以,如果我想要的函数叫做special_input,我使用了代码:

data = special_input('Prompt: ')
print data

than the output would be something like: 比输出更像是:

Prompt: Hello Hello

Yes, there are other ways to read a line like raw_input 是的,还有其他方法可以读取像raw_input这样的行

You can use sys.stdin() : 您可以使用sys.stdin()

import sys
line = sys.stdin.readline()

Or if you want to get a password you can also use getpass.getpass() : 或者,如果您想获得密码,您也可以使用getpass.getpass()

import getpass
line = getpass.getpass()

But if you want something more fancy you will need to use curses 但是如果你想要更加花哨的东西,你需要使用诅咒

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

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