Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每隔20分钟左右列出索引超出范围错误_Python_List_Indexing - Fatal编程技术网

Python 每隔20分钟左右列出索引超出范围错误

Python 每隔20分钟左右列出索引超出范围错误,python,list,indexing,Python,List,Indexing,在我正在编写的IRC bot中,我有两个函数,用于检索发送消息的用户和发送的消息。它通过将发送到bot的读取缓冲线分成两部分来实现这一点,然后使用第二个列表索引来检索用户/消息。以下是代码的第一部分: def getUser(line): if "!" in line: separate = line.split(":", 2) user = separate[1].split("!", 1)[0] return user else

在我正在编写的IRC bot中,我有两个函数,用于检索发送消息的用户和发送的消息。它通过将发送到bot的读取缓冲线分成两部分来实现这一点,然后使用第二个列表索引来检索用户/消息。以下是代码的第一部分:

def getUser(line):
    if "!" in line:
        separate = line.split(":", 2)
        user = separate[1].split("!", 1)[0]
        return user
    else:
        print "Nothing to receive from the server!"
def getMessage(line):
    if ':' in line:
        separate = line.split(":", 2)
        message = separate[2]
        return message
    else:
        print "Nothing to receive from the server!"
正如您所看到的,我试图形成一种方法,在服务器没有任何东西可接收时,阻止自己从服务器超时;这就是问题的根源。它将检索一行nothing,然后尝试拆分nothing,然后尝试访问超出范围的列表索引。我有一个想法,这可能可以通过使用
try/catch
语句来实现,但是我对Python不太熟悉,对此不太了解。下面是访问函数的代码的其余部分(如果有帮助):

readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()

for line in temp:
    user = getUser(line)
    message = getMessage(line)

我在行
message=getMessage(line)
user=getUser(line)
上得到了列表索引错误。一个简单的解决方案是在使用前检查行是否为空:

def getUser(line):
    if line:
        if "!" in line:
            separate = line.split(":", 2)
            user = separate[1].split("!", 1)[0]
            return user
    print "Nothing to receive from the server!"
def getMessage(line):
    if line:
        if ':' in line:
            separate = line.split(":", 2)
            message = separate[2]
            return message
    print "Nothing to receive from the server!"
空字符串、list(或None)的计算结果将为False,因此代码将停在那里并打印消息(或者,如果您不喜欢我所做的,可以重新构造if语句)。任何非空字符串/列表或文字将计算为“True”,并进行下一个if检查

编辑:

添加try/except:

def getUser(line):
    try:
        if line:
            if "!" in line:
                separate = line.split(":", 2)
                user = separate[1].split("!", 1)[0]
                return user
        print "Nothing to receive from the server!"
    except (IndexError as e):
        print e
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

我认为您的问题在于这些行(可能除了不检查
line
是否为空字符串之外,如gariepy的回答所示):

在第一种情况下,您假设
separate
至少有2个元素(在
separate[1]
上使用
split
),在第二种情况下,您假设
separate
至少有3个元素(在
separate[2]
上使用
split
)。在访问这些索引之前,应首先检查它们的长度,如下所示:

separate = line.split(":", 2)
if len(separate) >= 2:
    user = separate[1].split("!", 1)[0]


另外,在对其应用拆分之前,请记住检查
是否为非空字符串,如另一个答案所示。希望这有帮助。

您可以捕获
索引器
例外如果您能说出在哪一行收到错误,这将很有帮助。看看它,我想应该是这样的:user=separate[1]。split(“!”,1)[0]不太确定您在这里做什么。。。你能发布一个你正在使用的数据的例子吗?@davo36刚刚在问题的底部添加了这个。添加错误traceback@wong2由于短路,工作正常!我对此进行了测试,遗憾的是,它给了我同样的索引器。我添加了一个编辑来向您展示如何处理异常。
separate = line.split(":", 2)
message = separate[2]
separate = line.split(":", 2)
if len(separate) >= 2:
    user = separate[1].split("!", 1)[0]
separate = line.split(":", 2)
if len(separate) >= 3:
    message = separate[2]