python - 由于元组错误,无法访问多个嵌套字典中的字典

标签 python dictionary nested tuples

我有一个名为输出的数据框,看起来像 -

output
Out[48]: 
({'results': [{'alternatives': [{'confidence': 0.82,
      'transcript': 'thank you for calling a AA health insurance my name is Dick right the pleasure of speaking with '}],
    'final': True},
   {'alternatives': [{'confidence': 0.57, 'transcript': 'hi Nick this is '}],
    'final': True},
   {'alternatives': [{'confidence': 0.78,
      'transcript': 'hi Julie I think we talked earlier we did '}],
    'final': True},
  {'alternatives': [{'confidence': 0.86,
      'transcript': "thing else comes up or you have any questions just don't hesitate to call us okay okay thank you so much yeah you're very welcome you have a great rest your day okay you too bye bye "}],
    'final': True}],
  'result_index': 0},)

我试图仅访问“成绩单”并将其转换为 csv 数据框。我试过了-

output.to_csv("script.csv")
Traceback (most recent call last):

  File "<ipython-input-44-85a7c839323b>", line 1, in <module>
    output.to_csv("script.csv")

AttributeError: 'tuple' object has no attribute 'to_csv'

我也尝试只访问成绩单,但我得到了以下相同的错误-

print(output['results'][0]['alternatives'][0]['transcript'])

Traceback (most recent call last):

  File "<ipython-input-49-03a8e1a518ee>", line 1, in <module>
    print(output['results'][0]['alternatives'][0]['transcript'])

TypeError: tuple indices must be integers or slices, not str

如何避免此错误?

最佳答案

您可以从字典列表构建 pandas DataFrame。假设您只需要最里面的元素,您可以使用推导式来获取它:

df = pd.DataFrame([i for elts in output for alts in elts['results'] for i in alts['alternatives']])

您将获得以下 DataFrame:

   confidence                                         transcript
0        0.82  thank you for calling a AA health insurance my...
1        0.57                                   hi Nick this is 
2        0.78         hi Julie I think we talked earlier we did 
3        0.86  thing else comes up or you have any questions ...

关于python - 由于元组错误,无法访问多个嵌套字典中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60436354/

相关文章:

python - 使用 BeautifulSoup 或 golang colly 解析 HTML 时遇到问题

python - pandas 函数基于 dict 创建组合列

c# - 使用 AutoMapper 展平嵌套对象的更好方法?

delphi - Delphi 中的嵌套属性

python - 在 Python 中处理深度嵌套字典的便捷方法

javascript - 如何从键字符串构建嵌套属性

Python matplotlib 叠加散点图

python - 添加列表值时出现内存泄漏

python tensorflow 在输入层上使用 dropout

python - 使用循环从 Pandas 列构建 Python 字典