【问题标题】:TypeError: 'bool' object is not subscriptable on NLPTypeError:'bool' 对象在 NLP 上不可下标
【发布时间】:2021-07-16 10:07:58
【问题描述】:

当我尝试从泡菜中打开某些部分时,我得到了这个错误

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!'

【问题讨论】:

    标签: python pandas nlp boolean


    【解决方案1】:

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

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

    full_text.append(tweet['full_text'])
    

    【讨论】:

    • 您为什么在回答中谈论“retweeted_status”?您想正确提取推文的全文吗?那为什么不按我的建议做呢?
    • 因为我只转发了推文而不是完整的推文。这就是为什么
    • 这不是它的工作原理......你可以阅读这个:w3schools.com/python/python_dictionaries.asp
    • 我们在 API 高音单元上有两个不同的全文。第一篇全文我们只是转发了推文。在转推状态中,我们有一条完整的推文。
    • 我没有在您在帖子中打印的键中看到“retweeted_status”,因此可能没有使用正确的键或未在正确的对象上使用此键。也许您可以分享一条推文的示例 json 文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2019-02-20
    • 1970-01-01
    • 2016-07-20
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多