首页 > 解决方案 > 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!'

标签: pythonpandasnlpboolean

解决方案


发生这种情况是因为您试图获取full_text布尔值的项目,即tweet["retweeted"]

您可能想要删除["retweeted"]并且只有:

full_text.append(tweet['full_text'])

推荐阅读