从 JSON 推文元素中提取 'text' 字段并将其添加到字符串数组 python
Extracting the 'text' field from a JSON tweet element and adding it to string array python
我有一个 MongoDB 集合,里面装满了我收集的推文,现在我想对它们执行情绪分析,但我只想对每个推文的 'text' 字段执行此分析元素。我最初有一段代码来确定元素是否有文本字段,所以我修改了它以尝试检测它是否有文本字段,如果有则将它添加到数组的下一个元素但是我得到了类型错误如下所示。
appleSentimentText[record] = record.get('text')
TypeError: list indices must be integers, not dict.
我知道这意味着它与 [record] 不是整数有关,但我对如何将其变成整数感到困惑?我是 Python 的新手,因此非常感谢您的帮助。下面是我的代码片段供参考。
appleSentimentText = []
for record in db.Apple.find():
if record.get('text'):
appleSentimentText[record] = record.get('text')
列表要求其索引为整数,因此会出现错误。
如果要添加到列表中,请使用 list.append
或 list.insert
方法。
appleSentimentText.append(record.get("text"))
我有一个 MongoDB 集合,里面装满了我收集的推文,现在我想对它们执行情绪分析,但我只想对每个推文的 'text' 字段执行此分析元素。我最初有一段代码来确定元素是否有文本字段,所以我修改了它以尝试检测它是否有文本字段,如果有则将它添加到数组的下一个元素但是我得到了类型错误如下所示。
appleSentimentText[record] = record.get('text')
TypeError: list indices must be integers, not dict.
我知道这意味着它与 [record] 不是整数有关,但我对如何将其变成整数感到困惑?我是 Python 的新手,因此非常感谢您的帮助。下面是我的代码片段供参考。
appleSentimentText = []
for record in db.Apple.find():
if record.get('text'):
appleSentimentText[record] = record.get('text')
列表要求其索引为整数,因此会出现错误。
如果要添加到列表中,请使用 list.append
或 list.insert
方法。
appleSentimentText.append(record.get("text"))