python - 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() 函数上,有什么想法吗?
解决方案
a = "阿巴达巴德阿德巴德 "
a.split("1", 1)[1] Traceback(最近一次调用最后一次):文件“”,第 1 行,在 IndexError:列表索引超出范围
我认为您的问题出在您的字符串中,它不会拆分
推荐阅读
- php - 为什么我可以直接访问我的雄辩关系,但不能使用 with() 急切地加载它?
- firebase - Firebase 中的多个类,当我通过 Recycler Adapter 获取它时,“适配器”在不同的 if else 语句中声明。onStart 和 onStop 不起作用
- c++ - 使用char类型的glvalue访问另一种类型的对象时是否不需要导致UB
- python - 在 elmoformanylangs 中设置 config_path
- elasticsearch - 在 elasticSearch 中创建管道的问题
- google-analytics - 向客户发送请求访问邀请,以便在 Google Analytics 中获得用户访问权限
- r - ggplot2:将协方差矩阵绘制为没有行/列名的热图
- python - 将元组列表转换为熊猫数据框的单列?
- python - ValueError:allow_pickle=False 时无法加载对象数组
- arrays - 扫描和显示数组元素的函数出错