python - 类型错误 : sequence subscript must be integer or slice error in Jython

标签 python jython

我正在使用下面的代码并得到如下输出。请提出建议。

def isServerRunning(nodename,servername):
    """Returns a boolean to say if the server is running.
    Not 100% accurate but should be close - relies on there only being an mbean for
    a server if it's running"""

    mbean = AdminControl.queryNames('type=Server,node=%s,name=%s,*' % (nodename,servername))
    if mbean:
        return True
    return False

def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]


l = ['acsayul33450Node00', 'dmgr', 'acsayul33450Node01', 'gndtaskprd1f1', 'acsayul33450Node01', 'nodeagent', 'acsayul33451Node01', 'gndtaskprd1f2', 'acsayul33451Node01', 'nodeagent']
jvmList1 = chunks(l, 2)
sNameList = []
jvmList2 = []
N = 0
S = 0
for jvms in jvmList1:
    N += 1
    S = str(N)
    jvm = (jvms[1])
    node = (jvms[0])
    status = "stopped"
    jvmList2 = jvm
    if isServerRunning(node,jvm):
        status = "running"
        print("\n")
    sNameList.append(" %2s. %s %s %s" % (S,  jvm, node, status))
X = 1
while X:
        print("\n".join(sNameList))
        choice = raw_input("\nEnter your choice [1-%s] : " % (S))

        try:
            ServerName = jvmList2[choice]   
            X = 0
        except KeyError:
                print("\nInvalid entry\n")
print ServerName 

输出如下。

wsadmin>execfile('C:\IBM\pyscripts\test\10.py')


  1. dmgr acsayul33450Node00 running
  2. gndtaskprd1f1 acsayul33450Node01 stopped
  3. nodeagent acsayul33450Node01 stopped
  4. gndtaskprd1f2 acsayul33451Node01 stopped
  5. nodeagent acsayul33451Node01 stopped

Enter your choice [1-5] : 1
WASX7015E: Exception running command: "execfile('C:/IBM/pyscripts/test/10.py')";
exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
  File "C:/IBM/pyscripts/test/10.py", line 38, in ?
TypeError: sequence subscript must be integer or slice

最佳答案

ServerName = jvmList2[choice] 

您的选择是一个字符串。您应该首先将其转换为 int

choice = int(raw_input("\nEnter your choice [1-%s] : " % (S)))

关于python - 类型错误 : sequence subscript must be integer or slice error in Jython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17836125/

相关文章:

java - com.ibm.ws.scripting.adminCommand.AdminTask 到底在哪里?

python - 询问用户收集 gem 的天数

python - tfa.image.transform 的 PyTorch 等效函数

带有嵌套 range() 函数的 Python 片段

java - 如何使用jython jar将python模块添加到java中

python - sre_constants.error : nothing to repeat in jython

python - Matplotlib - MatplotlibDeprecationWarning

python - Django 可以用于 Web 服务吗?

python - 使用 Jython 安装 Pyodbc?

python - 在 Jython 中导入非标准的 python 模块