![](/img/trans.png)
[英]Python TypeError: sequence item 0: expected str instance, NoneType found
[英]TypeError: sequence item 5: expected str instance, NoneType found
我正在使用 Tweepy 到 Twitter 的 stream 推文。 目前,我正在嘗試實現在數據庫中更新用戶 ID 列表時斷開 stream 的代碼,並使用更新的列表打開一個新的 stream。
實現工作正常,但不知何故, .filter()
認為我傳遞的是None
而不是列表。 我可以在將列表傳遞給方法之前打印它。 那么我錯過了什么?
這是錯誤的來源: line 459, in filter self.body['follow'] = u','.join(follow).encode(encoding)
有些東西正在評估為無,我不知道它是什么。 有人救了我!!!
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
它說您傳遞的列表/序列中的第 6 個元素(第item 5
)是None
。 .join()
只能連接字符串,這會擾亂方法。 您的print(new_user_ids['user_ids'])
行的 output 應該可以幫助您找到問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.