如何解决类型错误:序列项 5:预期的 str 实例,找到 NoneType 问题:更多背景:代码:整个回溯在评论中要求
我正在使用 Tweepy 从 Twitter 流式传输推文。目前,我正在尝试实现在数据库中更新用户 ID 列表时断开流的代码,并使用更新后的列表打开一个新流。
问题:
实现工作正常,但不知何故,.filter()
认为我传递的是 None
而不是列表。我可以在将列表传递给方法之前打印它。那么我错过了什么?
更多背景:
这是错误的来源:line 459,in filter self.body['follow'] = u','.join(follow).encode(encoding)
有些东西正在评估为 None ,我不知道它是什么。谁能救救我!!!
代码:
def get_user_accounts():
global last_number_of_accounts
try:
# All psycopg2 and python mumbo jumbo that gives me back a list of twitter_user_ids
# That list is assigned to `new_user_ids`
number_of_accounts = len(new_user_ids)
if last_number_of_accounts == -1:
return {"has_changed": True,"user_ids": new_user_ids,"first_connection": True}
if number_of_accounts != last_number_of_accounts:
return {"has_changed": True,"first_connection": False}
else:
return {"has_changed": False,"first_connection": False}
except Exception as e:
print(e)
def check_number_of_accounts():
threading.Timer(3,check_number_of_accounts).start()
global last_number_of_accounts
global streamUserTweets
new_user_ids = get_user_accounts()
if new_user_ids['has_changed']:
print(new_user_ids['user_ids']) # This does not print None
last_number_of_accounts = len(new_user_ids['user_ids'])
if new_user_ids['first_connection']:
# Open stream for the first time
try:
streamUserTweets.filter(follow=new_user_ids['user_ids']) # So how is this none!!!
except Exception as e:
print(e)
else:
# Disconnect stream first,then connect with updated list
try:
streamUserTweets.disconnect()
streamUserTweets.filter(follow=new_user_ids['user_ids'])
except Exception as e:
print(e)
# TWITTER AUTHENTICATION STUFF
userTweetStreamListener = UserTweetStreamListener()
streamUserTweets = tweepy.Stream(auth=api.auth,listener=userTweetStreamListener)
last_number_of_accounts = -1
check_number_of_accounts()
整个回溯(在评论中要求)
Traceback (most recent call last):
File "path/to/file.py",line 247,in <module>
check_number_of_accounts()
File "path/to/file.py",line 208,in check_number_of_accounts
streamUserTweets.filter(follow=new_user_ids['user_ids'])
File "/path/to/env/path/to/tweepy/streaming.py",line 459,in filter
self.body['follow'] = u','.join(follow).encode(encoding)
TypeError: sequence item 5: expected str instance,NoneType found
解决方法
它并没有说你通过了ipady
:
None
它表示您传递的列表/序列中的第 6 个元素 (TypeError: sequence item 5: expected str instance,NoneType found
) 是 item 5
。 None
只能连接字符串,这会扰乱方法。 .join()
行的输出应该可以帮助您找到问题所在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。