Python,从input()中删除所需的引号

ric*_*n90 6 python

好吧,我一直在编写一个脚本,但是输入部分会发生令人讨厌的事情.根据我的python版本,我需要为我的输入包含引号,否则我不需要.使用python 2.7,我需要引号; 用python 3.3,我没有.例如:

filename = input('Enter Update File: ')
print(filename)
Run Code Online (Sandbox Code Playgroud)

使用python 2.7,我需要用引号括住我的输入,否则会引发NameError; 在python 3.3中,我没有.

有办法避免这种情况吗?

kin*_*all 12

在Python 2.x上你需要使用raw_input()而不是input().在旧版本的Python上,input()实际上会评估您键入的内容,这就是您需要引号的原因(就像您在Python程序中编写字符串一样).

Python 3.x和Python 2.x之间存在许多差异; 这只是其中之一.但是,您可以使用以下代码解决此特定差异:

try:
    input = raw_input
except NameError:
    pass

# now input() does the job on either 2.x or 3.x
Run Code Online (Sandbox Code Playgroud)