首页 > 解决方案 > Python split()列表索引超出范围与tweepy

问题描述

我正在使用 Tweepy 从 Twitter 中提取推文并自动回复它们,并且我想在用户推文的开头进行拆分,这样机器人就不会不断地@'ing 本身并在无限循环中回复。

这是我得到的错误:

Traceback (most recent call last):
  File "hello.py", line 47, in <module>
    text = text.split("@DungeonTrekker ", 1)[1]
IndexError: list index out of range

这是代码:

text = x.text
text = text.split("@DungeonTrekker ", 1)[1] # Split tweet

API.update_status("@" + x.user.screen_name + text +  x.user.name, x.id) # Tweet
print("Status Updated") # Console output

exclude.append(x.id) # Add to excluded list

当我使用给定的字符串作为输入运行代码时,就可以了。我还打印出了实际的字符串text,它正确地接收了字符串,所以问题出在 split() 函数上,有什么想法吗?

标签: pythontweepy

解决方案


a = "阿巴达巴德阿德巴德 "

a.split("1", 1)[1] Traceback(最近一次调用最后一次):文件“”,第 1 行,在 IndexError:列表索引超出范围

我认为您的问题出在您的字符串中,它不会拆分


推荐阅读