python - TypeError:'bool' 对象在 NLP 上不可下标
问题描述
当我尝试从泡菜中打开某些部分时,我收到错误此错误
TypeError: 'bool' object is not subscriptable
filename = r"Biden_tweets.pkl"
full_text = []
with open(filename,"rb")as file:
while True:
try:
tweet = pickle.load(file)
full_text.append(tweet['retweeted']['full_text'])
#print(tweet.keys())
except EOFError:
break
这些是打印的键:
print(tweet.keys())
dict_keys(['created_at', 'id', 'id_str', 'full_text', 'truncated', 'display_text_range', 'entities', 'metadata', 'source', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'in_reply_to_screen_name', 'user', 'geo', 'coordinates', 'place', 'contributors', 'is_quote_status', 'retweet_count', 'favorite_count', 'favorited', 'retweeted', 'possibly_sensitive', 'lang'])
当我编辑我的代码时:
full_text.append(tweet['retweeted_status']['full_text'])
我得到:KeyError: 'retweeted_status'
但是当我打开时:
tweet['retweeted_status']['full_text']'
'Barr must be held accountable - and the Biden administration must release the fully unredacted Mueller report!
我得到输出
{'created_at': 'Mon Dec 14 23:56:27 +0000 2020',
'id': 1338633986067795970,
'id_str': '1338633986067795970',
'full_text': 'RT @Amy_Siskind: Barr must be held accountable - and the Biden administration must release the fully unredacted Mueller report!
'retweeted_status': {'created_at': 'Mon Dec 14 23:40:32 +0000 2020',
'id': 1338629978334826503,
'id_str': '1338629978334826503',
'full_text': 'Barr must be held accountable - and the Biden administration must release the fully unredacted Mueller report!'
解决方案
发生这种情况是因为您试图获取full_text
布尔值的项目,即tweet["retweeted"]
您可能想要删除["retweeted"]
并且只有:
full_text.append(tweet['full_text'])
推荐阅读
- python-3.x - 根据 Python Dataframe 中具有特定要求的其他列的值创建新列
- vba - 为 Word VBA 使用自定义字体(非 MS Office)
- javascript - 打字稿:未定义的静态变量问题
- anaconda - 如何在最新 Anancoda 2019.03 中安装/更新到 sympy 1.4?
- express - 如何使用 socket.io 通过套接字登录/注销?
- c++ - 共享指针导致内存泄漏?
- c# - Is there a way that I can avoid an if statement full of checks
- python - 在笔记本中加载模块
- python - 如何提取数据框的所有电子邮件地址和电话号码?
- python - Keep QToolbar showing always all items