Python错误:索引错误:字符串索引超出范围搜索[1]

时间:2018-11-11 06:43:58

标签: python python-3.x

https://i.stack.imgur.com/MDNRm.png

def GetJumpFunc(search):
    path = GetPathByName(search[1])
    lines = ReadAllLines(path)
    for x in range(0, len(lines)):
        if ( search[0] in lines[x] and lines[x+3].find("jump") != -1):
            return GetStringBetwean2Chars(lines[x+4], "L", "")
    return ''

def GetPathByName(name):
    return ".\Transformice-0\{0}.class.asasm".format(name.replace("\\x", "%"))

def ReadAllLines(path):
    return ReadAllText(path).split('\n')

帮帮我吗? 我在第2行中收到错误消息:

  

path = GetPathByName(search [1])
  IndexError:字符串索引超出范围

1 个答案:

答案 0 :(得分:0)

search[1]假定字符串搜索 用至少包含2个字符的字符串填充。

尝试此操作退出一个空字符串。

def GetJumpFunc(search):
    if not search:
        return ''
    path = GetPathByName(search[1])

或仅退出此错误:

def GetJumpFunc(search):
    try:
        path = GetPathByName(search[1])
    except IndexError:
        return ''