Python split() 列表索引超出 tweepy 范围
Python split() list index out of range with 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() 函数上,有什么想法吗?
text = text.split("@DungeonTrekker ", 1)[1] # Split tweet
此行告诉文本在 @DungeonTrekker
的实例处拆分
如果@DungeonTrekker 位于该行的开头,那么它之前没有任何内容,之后是其余文本。索引 0 将是 '',索引 1 将是推文的其余部分。
但是,如果您正在使用其他用户的推文,则开头可能不会有 @DungeonTrekker
,因此除非在推文的文本中提及,否则您的索引将只有 0。
你可以在
(space) 处拆分并使用第一个索引来解决这个问题,这样无论用户名是什么(我不使用 twitter 但我假设space 不允许出现在用户名中),它会正确拆分它。
a = "ababdababd adbabd "
a.split("1", 1)[1]
追溯(最近一次通话):
文件“”,第 1 行,位于
IndexError: 列表索引超出范围
我认为你的问题出在你的字符串上,它不会分裂
我正在使用 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() 函数上,有什么想法吗?
text = text.split("@DungeonTrekker ", 1)[1] # Split tweet
此行告诉文本在 @DungeonTrekker
如果@DungeonTrekker 位于该行的开头,那么它之前没有任何内容,之后是其余文本。索引 0 将是 '',索引 1 将是推文的其余部分。
但是,如果您正在使用其他用户的推文,则开头可能不会有 @DungeonTrekker
,因此除非在推文的文本中提及,否则您的索引将只有 0。
你可以在
(space) 处拆分并使用第一个索引来解决这个问题,这样无论用户名是什么(我不使用 twitter 但我假设space 不允许出现在用户名中),它会正确拆分它。
a = "ababdababd adbabd "
a.split("1", 1)[1] 追溯(最近一次通话): 文件“”,第 1 行,位于 IndexError: 列表索引超出范围
我认为你的问题出在你的字符串上,它不会分裂