在python 2和3中使用input / raw_input [重复]

时间:2022-08-29 20:15:22

This question already has an answer here:

这个问题在这里已有答案:

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.

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?

6 个解决方案

#1


40  

Bind raw_input to input in Python 2:

将raw_input绑定到Python 2中的输入:

try:
   input = raw_input
except NameError:
   pass

Now input will return a string in Python 2 as well.

现在输入也会在Python 2中返回一个字符串。


If you're using six to write 2/3 compatible code then six.input() there points to raw_input() in Python 2 and input() in Python 3.

如果你使用6来编写2/3兼容代码,则six.input()指向Python 2中的raw_input()和Python 3中的input()。

#2


6  

Update: This method only works if you have future installed and the answers above are much better and more generalizable.

更新:此方法仅在您将来安装并且上述答案更好且更通用时才有效。

From this cheatsheet there is another method that looks cleaner:

从这个备忘单中有另一种看起来更干净的方法:

# Python 2 and 3:
from builtins import input

#3


4  

I think the best way to do this is

我认为最好的办法是

import six

six.moves.input()

...it'll work across 2 and 3.

......它可以在2和3之间工作。

#4


1  

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 int only, but won't be converted to string as 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的所有内容,作为字符串,其中input()保留给定参数的数据类型(即如果给定的参数是int类型,那么它将保持为仅限int,但不会像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 NameError will be thrown.

因此,在Python 2中使用input()时,用户在传递参数时必须小心。如果要传递一个字符串,则需要使用quote传递它(因为python将quote中的字符识别为字符串)。否则将抛出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.

为了避免Python 2中输入()所需的额外关注,它已在Python 3中删除。并且raw_input()已在Python 3中重命名为input()。来自Python 2的input()的功能不再是Python 3中的Python 3(input())提供了raw_input()在Python 2中的服务。

This post might be helpful for a detailed understanding.

这篇文章可能有助于详细了解。

#5


1  

Explicitly load the function:

显式加载函数:

from builtins import input

来自builtins导入输入

Then you can use input() in python2 as well as python3.

然后你可以在python2和python3中使用input()。

You may have to install the dependency:

您可能必须安装依赖项:

pip install future

pip安装未来

#6


1  

You can write your code in either python2 and use futurize or in python3 and use pasteurize. This removes the complexity of thinking about compatible code and guarantees good practices.

您可以在python2中编写代码并使用futurize或在python3中使用巴氏杀菌。这消除了考虑兼容代码的复杂性并保证了良好实践。

Regarding this specific question

关于这个具体问题

from builtins import input

Is exactly what the above scripts produce.

正是上面的脚本产生的。

#1


40  

Bind raw_input to input in Python 2:

将raw_input绑定到Python 2中的输入:

try:
   input = raw_input
except NameError:
   pass

Now input will return a string in Python 2 as well.

现在输入也会在Python 2中返回一个字符串。


If you're using six to write 2/3 compatible code then six.input() there points to raw_input() in Python 2 and input() in Python 3.

如果你使用6来编写2/3兼容代码,则six.input()指向Python 2中的raw_input()和Python 3中的input()。

#2


6  

Update: This method only works if you have future installed and the answers above are much better and more generalizable.

更新:此方法仅在您将来安装并且上述答案更好且更通用时才有效。

From this cheatsheet there is another method that looks cleaner:

从这个备忘单中有另一种看起来更干净的方法:

# Python 2 and 3:
from builtins import input

#3


4  

I think the best way to do this is

我认为最好的办法是

import six

six.moves.input()

...it'll work across 2 and 3.

......它可以在2和3之间工作。

#4


1  

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 int only, but won't be converted to string as 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的所有内容,作为字符串,其中input()保留给定参数的数据类型(即如果给定的参数是int类型,那么它将保持为仅限int,但不会像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 NameError will be thrown.

因此,在Python 2中使用input()时,用户在传递参数时必须小心。如果要传递一个字符串,则需要使用quote传递它(因为python将quote中的字符识别为字符串)。否则将抛出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.

为了避免Python 2中输入()所需的额外关注,它已在Python 3中删除。并且raw_input()已在Python 3中重命名为input()。来自Python 2的input()的功能不再是Python 3中的Python 3(input())提供了raw_input()在Python 2中的服务。

This post might be helpful for a detailed understanding.

这篇文章可能有助于详细了解。

#5


1  

Explicitly load the function:

显式加载函数:

from builtins import input

来自builtins导入输入

Then you can use input() in python2 as well as python3.

然后你可以在python2和python3中使用input()。

You may have to install the dependency:

您可能必须安装依赖项:

pip install future

pip安装未来

#6


1  

You can write your code in either python2 and use futurize or in python3 and use pasteurize. This removes the complexity of thinking about compatible code and guarantees good practices.

您可以在python2中编写代码并使用futurize或在python3中使用巴氏杀菌。这消除了考虑兼容代码的复杂性并保证了良好实践。

Regarding this specific question

关于这个具体问题

from builtins import input

Is exactly what the above scripts produce.

正是上面的脚本产生的。