在 python 2 和 3 中使用 input/raw_input
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21731043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of input/raw_input in python 2 and 3
提问by 218
I would like to set a user prompt with the following question:
我想用以下问题设置用户提示:
save_flag is not set to 1; data will not be saved. Press enter to continue.
save_flag 未设置为 1;数据不会被保存。按回车继续。
input()works in python3 but not python2. raw_input()works in python2 but not python3. Is there a way to do this so that the code is compatible with both python 2 and python 3?
input()适用于python3但不适用于python2。raw_input()适用于python2但不适用于python3。有没有办法做到这一点,以便代码与 python 2 和 python 3 兼容?
回答by Ashwini Chaudhary
Bind raw_inputto inputin Python 2:
在 Python 2 中绑定raw_input到input:
try:
input = raw_input
except NameError:
pass
Now inputwill return a string in Python 2 as well.
现在input也将在 Python 2 中返回一个字符串。
If you're using sixto write 2/3 compatible code then six.input()there points to raw_input()in Python 2 and input()in Python 3.
如果您要six编写 2/3 兼容代码,那么在 Python 2 和Python 3 中six.input()都有指向。raw_input()input()
回答by emschorsch
Update: This method only works if you have future installed and the answers above are much better and more generalizable.
更新:此方法仅适用于您将来安装并且上述答案更好且更具普遍性的情况。
From this cheatsheetthere is another method that looks cleaner:
从这个备忘单中,还有另一种看起来更干净的方法:
# Python 2 and 3:
from builtins import input
回答by Pabitra Pati
This is because, In python 2, raw_input()accepts everything given to stdin, as a string, where as input()preserves the data type of the given argument (i.e. if the given argument is of type int, then it will remain as intonly, but won't be converted to stringas in case of raw_input()). That is basically, when input()is used, it takes the arguments provided in stdin as string, and evaluates the same. And this evaluation converts the argument to corresponding type.
这是因为,在 python 2 中,将raw_input()所有提供给 stdin 的内容作为字符串接受,其中 asinput()保留给定参数的数据类型(即,如果给定参数是 type int,则它将保持为intonly,但不会是转换为string如 ) 的情况raw_input()。基本上,当input()使用时,它将 stdin 中提供的参数作为字符串,并对其进行评估。此评估将参数转换为相应的类型。
# Python 2.7.6
>>> a = raw_input("enter :- ")
enter :- 3
>>> type(a) # raw_input() converts your int to string
<type 'str'>
>>> a = input("enter :- ")
enter :- 3
>>> type(a) # input() preserves the original type, no conversion
<type 'int'>
>>>
Therefore, while using input()in Python 2, user has to be careful while passing the arguments. If you are passing a string, you need to pass it with quote ( since python recognizes characters inside quote as string). Else NameErrorwill be thrown.
因此,input()在 Python 2 中使用时,用户在传递参数时必须小心。如果你传递一个字符串,你需要用引号传递它(因为python将引号内的字符识别为字符串)。否则NameError会被抛出。
>>> a = input("enter name :- ")
enter name :- Derrick
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Derrick' is not defined
>>> a = input("enter name :- ")
enter name :- 'Derrick'
>>> a
'Derrick'
Whereas, if using raw_input(), you need not worry about the data type while passing the argument as everything it accepts as a string. But yes, inside your code you need to take care of appropriate type conversion.
然而,如果使用raw_input(),则在将参数作为字符串接受的所有内容传递时,您无需担心数据类型。但是,是的,在您的代码中,您需要处理适当的类型转换。
To avoid this extra care needed for input()in Python 2, it has been removed in Python 3. And raw_input()has been renamed to input()in Python 3. The functionality of input()from Python 2 is no more in Python 3. input()in Python 3 serves what raw_input()was serving in Python 2.
为了避免需要为此额外照顾input()在Python 2,已经在Python 3.取出并raw_input()已更名为input()出现在Python 3的功能input()在Python 2是没有更多的出现在Python 3input()在什么Python 3的发球raw_input()是在Python服务2.
This postmight be helpful for a detailed understanding.
这篇文章可能有助于详细了解。
回答by AlexG
Explicitly load the function:
显式加载函数:
from builtins import input
from builtins import input
Then you can use input()in python2 as well as python3.
然后你可以input()在python2和python3中使用。
You may have to install the dependency:
您可能必须安装依赖项:
pip install future
pip install future
回答by joedborg
I think the best way to do this is
我认为最好的方法是
import six
six.moves.input()
...it'll work across 2 and 3.
...它将在 2 和 3 中工作。
回答by gbonetti
You can write your code in either python2 and use futurizeor in python3 and use pasteurize. This removes the complexity of thinking about compatible code and guarantees good practices.
您可以在 python2 中编写代码并使用futurize或在 python3 中使用pasteurize。这消除了考虑兼容代码的复杂性并保证良好的实践。
Regarding this specific question
关于这个具体问题
from builtins import input
Is exactly what the above scripts produce.
正是上述脚本产生的。

