TypeError: sequence item 5: expected str instance, NoneType found
TypeError: sequence item 5: expected str instance, NoneType found
我正在使用 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, "user_ids": new_user_ids, "first_connection": False}
else:
return {"has_changed": False, "user_ids": new_user_ids, "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
它不会说你正在通过None
:
TypeError: sequence item 5: expected str instance, NoneType found
它表示您传递的 list/sequence 中的第 6 个元素 (item 5
) 是 None
。 .join()
只能连接字符串,这打乱了方法。 print(new_user_ids['user_ids'])
行的输出应该可以帮助您找到问题所在。
我正在使用 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, "user_ids": new_user_ids, "first_connection": False}
else:
return {"has_changed": False, "user_ids": new_user_ids, "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
它不会说你正在通过None
:
TypeError: sequence item 5: expected str instance, NoneType found
它表示您传递的 list/sequence 中的第 6 个元素 (item 5
) 是 None
。 .join()
只能连接字符串,这打乱了方法。 print(new_user_ids['user_ids'])
行的输出应该可以帮助您找到问题所在。