python - Python 2.7 的 input() 和 raw_input() 问题。用户输入的数据未正确读取。到底是怎么回事?
问题描述
所以在我的公司,他们让我使用 python 2.7 是出于兼容性的原因,我不会在这里讨论。
因此,我正在编写一个程序,该程序使用 SSH(特别是交换机)连接到设备,并且我能够使用 SSH 实际访问该设备,并且该设备可以在我的机器上 ping 通。问题 ?raw_input 似乎没有把它当作一个字符串。当我尝试 input() 时,它给了我一个无效的语法错误。
对于我编写的脚本,我通常使用 arparse 并且用户通过终端输入 IP 地址、用户名和密码,但我希望这个脚本不使用 argparse 而使用 input() 或 raw_input。我所有的 SSH 脚本都运行良好,除了这个,唯一一个使用 raw_input 和 input() 而不是 argparse
def runMain():
scriptName = os.path.basename(__file__)
print("The name of this script is: " + scriptName)
print("**************************************\n")
print("This script allows you to enable and disable ports on the SNET or SOOBM switches, have fun ! \n")
print("**************************************\n")
optionPrinter_switches_top()
user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
if user_input == 1:
print("You selected the SNET switch, lets proceed !")
deviceIP = input("Enter the IP address for this device") # I tried changing these to raw_input, I get a syntax issue
deviceUsername = input("Enter the username for this device")
devicePassword = input("Enter the password for this device")
confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.
if confirm_ping:
ssh_SNET = connectToSSH_SNET(deviceIP, deviceUsername, devicePassword)
else:
print("Sorry, that device is not even ping-able. Figure that issue out and retry the program...")
sys.exit(-1)
while True:
SNET_options()
user_choice_SNET = input("Please select an option")
switch_result = SNET_switch_func(user_choice_SNET)
if switch_result == "displayInterfaceBrief":
time.sleep(5)
displayInterfaceBrief_SNET(ssh_SNET)
elif switch_result == "enablePort":
time.sleep(5)
enablePort_SNET(ssh_SNET)
elif switch_result == "disablePort":
disablePort_SNET(ssh_SNET)
elif switch_result == "switchReboot":
reboot_SNET_switch(ssh_SNET)
else:
print("Exiting program now....")
sys.exit(-1)
以下是相关问题:
user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
deviceIP = input("Enter the IP address for this device") # I tried changing these to raw_input, I get a syntax issue
deviceUsername = input("Enter the username for this device")
devicePassword = input("Enter the password for this device")
confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.
结论 ?input()/raw_input() 存在问题。这里发生了什么,我该如何解决这个问题?我不能使用 python 3.7,这真的很令人沮丧。谢谢您的帮助
解决方案
尝试更改if user_input == 1:
为if int(user_input) == 1:
,因为输入函数默认接受字符串格式的输入。
而且,如果您想在 python 2.x 中使用input()
而不是raw_input()
从用户那里获取输入,那么您可以尝试以下代码:
if hasattr(__builtins__, 'raw_input'):
input=raw_input
user_input = input("Enter a number: ")
推荐阅读
- android - WearableRecyclerView .scrollToPosition 不适合磨损?
- xpath - XPath 删除单个节点(通过 Saxon CLI)
- karate - 我们可以使用“@ContinueNextStepsOnException”来运行空手道脚本中的所有步骤,而不是 karate.match(actual, expected)
- mysql - mysql 检查给定的字符串是否在没有 FIND_IN_SET 的逗号分隔列表中
- docker - 用于有状态应用程序的 Kubernetes 部署
- mysql - 在mysql中使用管道作为分隔符导出到磁盘csv
- node.js - 如何在 Nodejs 中连接多个应用程序....(Nodejs 在行动)
- microsoft-cognitive - 必应文本到语音中的语音名称规则
- sql - 当我们对已经重新分区的数据帧进行重新分区时会发生什么?
- android - 不允许加载本地资源:ionic 3 android